Multithreading in Java

Thread

Thread is a light-weight sub process. It can run concurrently with the other parts.

Multithreading

The process of executing multiple threads simultaneously is known as multithreading.
  • Multithreading means executing two parts of the same program concurrently.
  • It is a part of multitasking.
  • A program may contain more than one thread.
For Example: A browser can serve a web page while downloading a file. Each of these actions is a thread.

Life Cycle of Thread

life cycle of thread

New State: Soon after the creation of a thread, it is said to be in the new state.
Runnable State: The life of a thread starts after invoking the start () method. The start() method invokes the run () method.
Running State: When the thread is currently running, it is in the state of running.
Wait State: When other threads are holding the resources, the current thread is said to be in wait state
Dead State: When the thread has completed the execution of run () method, it is said to be in dead state.

Creating Thread

There are two ways to create a thread in Java.
1. Extending the thread class.
2. Implementing runnable interface.

1. Extending Thread Class
Thread class has constructor and method to create and perform the operation on class.

Program on using thread class

// MainThread.java

public class MyThread extends Thread
{
      String message;
      MyThread(String msg)
      {
            message=msg;
      }
      public void run()
      {
            for(int count=0;count<=5;count++)
            {
                  System.out.println("Run method: " + message);
            }
      }
}
public class MainThread
{
      public static void main(String[] args)
      {
            MyThread t1 = new MyThread("Run");
            MyThread t2 = new MyThread("Thread");
            t1.start();
            t2.start();
      }
}


Output:
Run method: Run
Run method: Run
Run method: Run
Run method: Run
Run method: Run
Run method: Run
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Thread

2. Implementing Runnable Interface
Runnable interface is available in java.lang package. The purpose of Runnable interface is to provide a set of rules common for objects to execute the functionality while they are active.

Example : Sample program to implement runnable interface

class MyThread implements Runnable
{
      String message;
      MyThread(String msg)
      {
            message = msg;
      }
      public void run()
      {
            for(int count=0;count<=5;count++)
            {
                   System.out.println("Run method: " + message);
            }
      }
}
public class MainMyThread
{
      public static void main(String[] args)
      {
             MyThread dt1=new MyThread("Run");
             MyThread dt2=new MyThread("Thread");
             Thread t1 = new Thread(dt1);
             Thread t2 = new Thread(dt2);
             t1.start();
             t2.start();
      }
}


Output:
Run method: Thread
Run method: Run
Run method: Thread
Run method: Run
Run method: Thread
Run method: Run
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Run
Run method: Run
Run method: Run