Yield , Join and Sleep methods in Thread | By Java4u

Yield , Join and Sleep method in Thread

We can prevent a thread by using following methods.

  1. yield()
  2. join()
  3. sleep()

 

1. yield():

  • yield() method causes to pass current executing thread to give the chance for waiting threads of same priority.
  • If there is no waiting thread or all waiting threads have low priority than same thread can continue it's execution. 
  • If multiple threads are waiting with same priority then which waiting thread will get the chance, we cannot know, it depends on Thread scheduler.
  • The thread which is yielded, when it will get the chance once again, it depends on Thread scheduler. and we cannot know exactly.
yield() method in thread java

Ex:

Yield method example in thread

  • In above program if we are commenting line no 20, then both thread will be executed simultaneously and we cannot know which thread will get chance first.
  • If if we are not commenting line 20 then child thread always calls yield() method because of that main thread will get chance more number of times and the chance of completing main thread is high.

Note:  Some platform would not provide proper support for yield() method.

2. join():

  •  If a thread wants to wait completing some other thread, then we use join() method.
Ex: If a thread t1 want to wait until completing t2 then t1 has to call t2.join().
  •  If t1 execute t2.join(), then immediately t1 will be entered into waiting state until t2 complete.
  • Once t2 complete then t1 can continue its execution.

Wedding card printing thread ( t2 ) has to wait until venue fixing thread ( t1 ) completion. Hence t2 has to call t1.join()

public final void join()  throws InterruptedException
 
public final void join( long ml )  throws InterruptedException
 
public final void join(long ml, int ns)  throws InterruptedException

Note:

Every join() method throws InterruptedException which is checked exception hence we should compulsory handle this  exception either by using try catch or by throws keyword. Otherwise we will get compile time error.

join() method in thread java
 

Case 1.  

join() method in thread java

Case 2.  Waiting of child thread until completing main thread.

join() method in thread java

In the above program child thread calls on main thread object hence child thread has to wait until completing main thread.
 

Case 3. 

If main thread call join() method on child thread object and child thread calls join() on main thread object then both thread will wait forever and program will be stacked (  like deadlock )

Case 4.  

If a thread calls join() method on the same thread itself then the program will be stacked ( like deadlock
 
Ex:
 
join() method example in thread
 

3. sleep():

If a thread do not want to perform any operation for a particular amount of time, then we should for a particular amount of time, then we should go for sleep method. 

sleep() method example in thread

Note: 

Every sleep method throws InterruptedException which is checked exception, hence whenever we are using sleep method compulsory we should handle this exception either by try catch or throws keyword.

sleep method in thread java

sleep method example in thread java

 

Comparison of yield, join and sleep methods in thread

 

 

 Related Post

RAKESH RAKA

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

1 Comments

  1. Casino Games - DrmCD
    Casino Games. Play for free. Discover 하남 출장안마 the thrill 순천 출장안마 of 인천광역 출장샵 gambling on the casino floor. Your 통영 출장샵 passion for the games that are free. Experience the thrill of gaming Feb 15, 남양주 출장마사지 2022Crazy Star: Casino Game Nov 1, 2022Crazy Star: Casino Game Nov 18, 2022Crazy Star: Casino Game

    ReplyDelete
Post a Comment
Previous Post Next Post