A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

MultiThreading

What is Multithreading?

What is MultiThreading?

MultiThreading in Java is a technique, which is used to for multi-tasking. Just like when a user wants to do multiple works at a time on the single system, then this type of work is called multi-tasking.
For example, Navneet wants to listen to music on her computer, at the same time she wants to download a song from The Internet, and also she wants to write a mail to her boss in the office.

Who Performs Multithreading ?

The answer is Threads.
How this thread works and How can we use these thread in our program to perform multithreading concept.
There are two ways to use Thread in Java:

1. By extending Thread class 2. By implementing Runnable interface
By extending Thread Class:
Example:

class myThread extends Thread
{
    public void run()
    {
        for(int i=0; i<10; i++)
        {
            System.out.println("child thread");
        }
    }
}

class ThreadDemo
{
    public static void main(String [] args)
    {
        myThread t = new myThread();
        t.start();
        for(int i = 0; i<10; i++)
        {
            System.out.println("Main thread");
        }
    }
}


Consider myThread class  which extends Thread class, in this class, a run method is implemented without any argument passing. 
Another ThreadDemo Class is created in which main method is called, 
Now, the following code is written in the example for object instantiation of myThread Class, 
myThread t = new myThread();

Now, another code  t.start(); is written for thread start. This start( ) method is already implemented in the Thread Class.

OUTPUT:




LEARN TUTORIALS

.

.