Thread Priorities:
- Every Thread in java has some priority, it may be default priority generated by JVM or customised priority provided by programmer.
- The valid range of Thread priorities is 1 to 10, where 1 is MIN_PRIORITY and 10 is MAX_PRIORITY Thread class defines the following constants to represent some standard priorities.
- Thread.MIN_PRIORITY -> 1
- Thread.NORM_PRIORITY -> 5
- Thread.MAX_PRIORITY -> 10
- Thread schedular will use priorities while allocating processor.
- The Thread which is having highest priority will get chance first.
- If two Threads having same priority then we can't expect exact execution order. It depends upon Thread schedular.
Thread class defines the following methods to get and set priority of a Thread
Otherwise RuntimeException : IllegalArgumentException
Default Priority:
The Default priority only for the main Thread is 5. But, for all remaining Threads default priority will be inherited from parent to child i.e., whatever priority parent Thread has the same priority will be there for the child Thread.
If we are commenting line-1, then both main and child threads have the same priority 5. Hence, we can't expect execution order and exact output.
If we are not commenting line-1, then both main Thread has priority 5 and child Thread has priority 10. Hence, child Thread will get chance first followed by main Thread.
In this case Output is
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Note: Some platforms won't provide proper support for Thread priorities.
We can prevent a Thread execution by using the following methods.
1. yield()
2. join()
3. sleep()
yield() :
- yield() method causes to pause current executing Thread to give the chance for remaining waiting Threads of same priority.
- If there is no waiting Thread or all waiting Threads have low priority then same Thread can continue its execution.
- If multiple Threads are waiting with same priority, then which waiting Threading will get the chance? we can't expect, it depends on Thread Schedular.
- The Thread which is yielded, when it will get chance once again, it depends on Thread Schedular and we can't expect exactly.
- In the above program if we are commenting line-1 then both the Threads will executed simultaneously and we can't expect which Thread will complete first.
- If we are not commenting line-1 then child Thread always called yield() method because of that main Thread will get chance more number of times and the chance of completing main Thread first is high.
NOTE: Some platforms (OS) won't provide proper support for yield() method.
join() :
- If a Thread wants to wait until completing some other Thread then we should go for join() method.
- For example, if a Thread t1 wants to wait until completing Thread t2 then t1 has to call t2.join().
- If t1 executes t2.join() then immediately t1 will be entered into waiting state until t2 completes.
- Once t2 completes then t1 can continue its execution.
Note: Every join() method throws InterruptedException which is checked Exception. Hence compulsory we should handle this exception either by using try-catch or by throws keyword. Otherwise, we will get compile time error
Case:1 waiting of main Thread until completing child Thread
- If we comment line-1 then both main and Child Threads will be executed simultaneously and we can't expect exact output.
- If we are not comment line-1 then main Thread calls join() method on child Thread object. Hence main Thread will wait until completing child Thread.
In this case output is
Seetha Thread
Seetha Thread
Seetha Thread
Seetha Thread
Seetha Thread
Rama Thread
Rama Thread
Rama Thread
Rama Thread
Rama Thread
Case 2: Waiting of child Thread until completing main Thread:
In the above example, Child Thread calls join() method on main Thread object. Hence Child Thread has to wait until completes main Thread.In this case output is
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Case 3:
If main Thread calls join() method on child Thread object and child Thread calls join() method on main Thread object then both the Threads will wait forever and the program will be structed. (This is something like Deadlock).
Case 4:
If a Thread calls join() method on the same Thread itself then the program will be structed(This is something like deadlock). In this case Thread has to wait infinite amount of time.
sleep():
If a Thread don't want to perform any operation for a particular amount of time then we should go for sleep() method.
Note: Every sleep() methods throws InterruptedException, which is checked Exception. Hence whenever we are using sleep() method compulsory we should handle InterruptedException either by try-catch or throws keyword otherwise we will get compile time error.
Output:
Slide - 0
Slide - 1
Slide - 2
Slide - 3
Slide - 4
How a Thread can interrupt another Thread???
A Thread can interrupt a sleeping Thread or waiting Thread by using interrupt() method of Thread class.
Syntax :Note: If we comment line-1 then main Thread won't interrupt child Thread. In this case child Thread will execute for loop 5 times.
If we are not commenting line-1 then main Thread interrupts child Thread.
In this case output is
End of main Thread
Lazy Thread
I got interrupted
Note:
- Whenever we are calling interrupt method if the target Thread is not in sleeping or waiting state then there is no impact of interrupt call immediately. interrupt call will be waited until target Thread entered into sleeping or waiting state.
- If the target Thread entered into sleeping or waiting state then immediately interrupt call will interrupt target Thread.
- If the target Thread never entered into sleeping or waiting state in its life time then there is no impact in interrupt call. This is the only case where interrupt call will be wasted.
In the above example interrupt call waited until child Thread completes for loop 10000 times.
property | yield() | Join() | sleep() |
---|---|---|---|
purpose | if a thread wants to pass its execution to give the chance for remaining Threads of same priority then we should go for yield() | a Thread wants to wait until completing some other Thread then we should go for join() | if a Thread wants to perform any operation for a particular amount of time then we should go for sleep() |
is it overloaded | NO | YES | YES |
is it final method | NO | YES | NO |
is it throws IE | NO | YES | YES |
is it native | YES | NO | sleep(long ms) -- native sleep(long ms, int ns) |
0 Comments