Java


Java Tutorial


Admission Enquiry Form

  

Java Multithreading




What is Multithreading in Java?

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
Each part of such program is called a thread.




What is Thread in Java?

A thread is a small and lightweight subprocess, the smallest unit of processing. It has a separate path of execution.
Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. It uses a shared memory area.


Threads can be created by using two mechanisms :
  • Extending the Thread class
  • Implementing the Runnable Interface



Life Cycle of a Thread in Java?

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.



Following are the stages of the life cycle −
New born state− A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a new born thread.

Runnable state − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting state− Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

Terminated (Dead state) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.




Thread Priorities in Java?

Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent.

Thread creation by extending the Thread class
We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.




Example using Extending the Thread class

class Thread1 extends Thread
{
int i;
public void run()
{
for(i=1;i<=20;i++)
{
System.out.println("Inside Thread1 "+i);
}
System.out.println("End of Thread1");
}
}
class Thread2 extends Thread
{
int i;
public void run()
{
for(i=1;i<=20;i++)
{
System.out.println("Inside Thread2 "+i);
}
System.out.println("End of Thread2");
}
}
class Thread3 extends Thread
{
int i;
public void run()
{
for(i=1;i<=20;i++)
{
System.out.println("Inside Thread3 "+i);
}
System.out.println("End of Thread3");
}
}
public class MultiThredingEx {
public static void main(String[] args)
{
Thread1 th1=new Thread1();
Thread2 th2=new Thread2();
Thread3 th3=new Thread3();
th1.start();
th2.start();
th3.start();    
}    //end of main
}//end of main class


Output:

Inside Thread3 1
Inside Thread3 2
Inside Thread3 3
Inside Thread2 1
Inside Thread2 2
Inside Thread2 3
Inside Thread2 4
Inside Thread2 5
Inside Thread1 1
Inside Thread1 2
Inside Thread3 4
Inside Thread3 5
Inside Thread2 6
Inside Thread1 3
Inside Thread1 4
Inside Thread1 5
Inside Thread1 6
Inside Thread3 6
Inside Thread2 7
Inside Thread1 7
Inside Thread1 8
Inside Thread1 9
Inside Thread1 10
Inside Thread3 7
Inside Thread2 8
Inside Thread1 11
Inside Thread3 8
Inside Thread3 9
Inside Thread2 9
Inside Thread2 10
Inside Thread2 11
Inside Thread1 12
Inside Thread3 10
Inside Thread3 11
Inside Thread2 12
Inside Thread2 13
Inside Thread2 14
Inside Thread2 15
Inside Thread2 16
Inside Thread1 13
Inside Thread1 14
Inside Thread1 15
Inside Thread1 16
Inside Thread1 17
Inside Thread3 12
Inside Thread2 17
Inside Thread1 18
Inside Thread3 13
Inside Thread3 14
Inside Thread3 15
Inside Thread3 16
Inside Thread3 17
Inside Thread2 18
Inside Thread2 19
Inside Thread1 19
Inside Thread1 20
Inside Thread3 18
Inside Thread3 19
Inside Thread2 20
End of Thread2
End of Thread1
Inside Thread3 20
End of Thread3



Example of Thread Priorities

class Thread1 extends Thread
{
int i;
public void run()
{
for(i=1;i<=20;i++)
{
System.out.println("Inside Thread1 "+i);
}
System.out.println("End of Thread1");
}
}
class Thread2 extends Thread
{
int i;
public void run()
{
for(i=1;i<=20;i++)
{
System.out.println("Inside Thread2 "+i);
}
System.out.println("End of Thread2");
}
}
class Thread3 extends Thread
{
int i;
public void run()
{
for(i=1;i<=20;i++)
{
System.out.println("Inside Thread3 "+i);
}
System.out.println("End of Thread3");
}
}
public class MultiThredingEx {
public static void main(String[] args)
{
Thread1 th1=new Thread1();
Thread2 th2=new Thread2();
Thread3 th3=new Thread3();
th3.setPriority(Thread.MAX_PRIORITY);
th2.setPriority(Thread.NORM_PRIORITY);
th1.setPriority(Thread.MIN_PRIORITY);
th1.start();
th2.start();
th3.start();    
}  //end of main
}//end of main class


Output:

Inside Thread3 1
Inside Thread3 2
Inside Thread3 3
Inside Thread2 1
Inside Thread2 2
Inside Thread2 3
Inside Thread2 4
Inside Thread2 5
Inside Thread2 6
Inside Thread2 7
Inside Thread2 8
Inside Thread2 9
Inside Thread2 10
Inside Thread2 11
Inside Thread2 12
Inside Thread2 13
Inside Thread2 14
Inside Thread1 1
Inside Thread3 4
Inside Thread3 5
Inside Thread3 6
Inside Thread3 7
Inside Thread3 8
Inside Thread3 9
Inside Thread3 10
Inside Thread3 11
Inside Thread3 12
Inside Thread3 13
Inside Thread3 14
Inside Thread3 15
Inside Thread3 16
Inside Thread3 17
Inside Thread3 18
Inside Thread3 19
Inside Thread3 20
End of Thread3
Inside Thread2 15
Inside Thread2 16
Inside Thread2 17
Inside Thread2 18
Inside Thread2 19
Inside Thread2 20
End of Thread2
Inside Thread1 2
Inside Thread1 3
Inside Thread1 4
Inside Thread1 5
Inside Thread1 6
Inside Thread1 7
Inside Thread1 8
Inside Thread1 9
Inside Thread1 10
Inside Thread1 11
Inside Thread1 12
Inside Thread1 13
Inside Thread1 14
Inside Thread1 15
Inside Thread1 16
Inside Thread1 17
Inside Thread1 18
Inside Thread1 19
Inside Thread1 20
End of Thread1