Thread in Java | Multithreading in Java

Thread in Java

Multitasking:

Executing several tasks simultaneously is the concept of multitasking.

There are 2 types of multitasking

  1. Process based
  2. Thread based

 

1. Process Based:-

 Executing several task simultaneously where each task is a separate independent program ( process ).
 
Ex-
  • Make a program 
  • Listen music 
  • Download a file 
(Same time in system, each task independent to each other in different process) 
 
 

2. Thread Based:- 

Executing several task simultaneously  where each task is a separate independent part of the same program, is called thread based multitasking and each independent is called thread.

Thread based multitasking is best at programming level.

When compared with old language developing multi-threaded in java  is very easy because java provide inbuilt support for multi-threading with reach API  [ Thread, Runnable, ThreadGroup... ]


Thread:

 Thread is a separate flow of execution. For every thread there is a separate job.

We can define a thread in the following 2 ways.

  • By extending Thread class.
  • By implementing Runnable interface.

1.   By extending Thread class:-

 
Create Thread by extends Thread class

Case 1:-  

Thread Scheduler

  • It is the part of JVM.
  • It is responsible to schedule threads. If multiple threads are waiting to get chance of execution, then in which order thread will be executed is decided by thread scheduler.
  • We cannot expect exact algorithm followed by thread scheduler. It is varied from JVM to JVM. Hence we cannot accept threads execution order and output.
  • Hence whenever situations comes to multi-threading there is no guarantee  for exact output but we can provide several possible output.

Case 2:-  

Difference between t.start() and t.run() method

  •  In the case of t.start(), a new thread will be created which is responsible for the execution of run() method.
  •  But in the case of t.run() method, a new thread would not be created run method will be executed just like a normal method call by main thread.
  • In the above program if we replace t.start() with t.run() then the output is [ child thread 10 times and main thread 10 times
  • Total output produce by only main thread.

 

Case 3:- 

 Importance of Thread class start() method

  • Thread class start() method is responsible to register the thread with Thread scheduler and all other mandatory activities.
  •  Hence without executing Thread class start(), there is no chance of starting a new thread in java.
  • Due to this Thread class start() is considered of heart multi-threading.  
start ()
{
  1. Register this thread with Thread Scheduler.
  2. Perform all other mandatory activities. 
  3. Invoke run().
}

 

Case 4:- 

 Overloading of run() method

  • Overloading of run() method is always possible but thread class start() method can invoke no-argument run() method.
  • The other overloaded method we have to call explicitly like a normal method call.
 Ex:
 
run() method in thread class

Case 5:- 

 If we are not overriding run() method

  •  If we are not overriding run() method then thread class run method will be executed which has empty implementation. Hence we will not get any output.
Ex:
 
 
Thread class run() method example

 

 Note: 

It is highly recommended to override run method otherwise do not go for multi-threaded concept.

Case 6:- 

 Overriding of start() method

If we override start() method, then our start() will be executed just like a normal method call and new thread would not be created
 
 
Start method in thread class

Case 7:- 

 Life cycle of thread

 
Life cycle of Thfread in java

 

Case 8:-  

After starting a thread if we are trying to restart the same thread then, we will get runtime exception saying - IllegalThreadException
 
Ex:
 
Thread t = new Thread();

 t.start();
.
.
.
t.start();  -->  RE: IllegalThreadException
 
 

2.Defining a thread by implementing Runnable ( Interface ):-

  •  We can define a thread by implementing Runnable interface.
 
Thread using Runnable in java

 
  •  Runnable interface present in java.lang package and its contains only one method 
public void run()


Ex:
 
 
Create thread using Runnable interface in java

Case study

MyRunnable r = new MyRunnable();

Thread t1 = new Thread();

Thread t2 =  new Thread(r);

 

Case 1:-  

     t1.start() 

A new thread will be created and which is responsible for execution of Thread class run method, which has empty implementation.
 

Case 2:-  

     t1.run()

 No new thread will be created and Thread class run() method will be executed just like a normal method call.


Case 3:-  

     t2.start()

A new thread will be created and which is responsible for execution of MyRunnable run() method.
 

Case 4:-  

     t2.run()

 A new thread would not be created and MyRunnable run() method will be executed just like a normal method call.
 

Case 5:-  

     r.start()

We will get compile time error saying:  
MyRunnable class does not have start capability.
CE:  cannot find symbol: method start(), location class MyRunnable


Case 6:-  

     r.run()

No new thread will be created and MyRunnable run() method will be executed like normal method call.

Which approach is best to define a thread

  • Among 2 ways of defining a thread implements Runnable approach is recommended. 
  • In first approach our class always extends Thread class, there is no chance of extending any other class. Hence we are missing  inheritance benefits. 

RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post