Interrupting a thread in Java | interrupt method in Thread Java

interrupt method in Thread Java

A thread can interrupt a sleeping  or waiting thread by using interrupt() method of Thread class.

public void interrupt()

Ex:
 
interrupt method example in thread java

 

Note ( Imp* )

  • Whenever we are calling interrupt() method and if target Thread is not in sleeping state 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 the target thread.
  • If the target thread never entered into sleeping or waiting state in the lifetime the, there is no impact of interrupt() call. This is only case where interrupt() call will be wasted.
 
Ex:
 
interrupt method in thread java example

 

RAKESH RAKA

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

Post a Comment (0)
Previous Post Next Post