Thread in Java
Multitasking:
Executing several tasks simultaneously is the concept of multitasking.
There are 2 types of multitasking
- Process based
- 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.
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 (){
- Register this thread with Thread Scheduler.
- Perform all other mandatory activities.
- 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:
- 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:
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
Case 7:-
Life cycle of thread
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.
- Runnable interface present in java.lang package and its contains only one method
public void run()
Ex:
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.