Java Thread: sleep() and interrupt() methods

Category : Java | Sub Category : Java Threads | By Prasad Bonam Last updated: 2021-04-20 03:53:26 Viewed : 581


Java Thread: sleep() and interrupt() methods

sleep()

Thread.sleep() method will  hold the execution of the currently running thread for a defined period of time. once the sleeping time os over, it will resume the execution. sleep() method is a static method available in java thread class. unlike wait() method, sleep method will not release any object monitor before going to sleep. it is guaranteed to sleep the thread for a given time period unless some other thread interrupt it (by calling interrupt() method on the thread object).

 

public class Application
{
public static void main(String[] args)
{
System.out.println("main thread is started");
try {
System.out.println("main thread will be slept for 5 seconds");
Thread.sleep(5000);
System.out.println("main thread resumed after sleeping");
} catch (InterruptedException ex) {
System.out.println("sleeping thread get interrupted");
}
System.out.println("main thread completed");
}
}

  

The execution of the above program will produce the following output.

main thread is started
main thread will be slept for 5 seconds
main thread resumed after sleeping
main thread completed

 

As you can see that the main thread is started and sleeping for 5 seconds. The  application(main thread) will sleep for 5 seconds and wake up after sleeping duration over.

 

Interrupting the sleeping thread with interrupt()

The interrupt() method can be called on a sleeping thread object to interrupt its sleep and move back to the runnable state.  The below example will demonstrate how the interrupt() is really working.

public class Application
{
public static void main(String[] args)
{
System.out.println("main thread is started");
ThreadA threadA = new ThreadA();
threadA.start();
ThreadB threadB = new ThreadB(threadA);
threadB.start();
System.out.println("main thread completed");
}
}
class ThreadA extends Thread
{
public void run()
{
System.out.println("ThreadA is running");
try {
System.out.println("ThreadA is sleeping for 15 seconds ");
Thread.sleep(15000);
System.out.println("ThreadA is waked up ");
} catch (InterruptedException e) {
System.out.println("Sleep of ThreadA get interrupted ");
}
System.out.println("ThreadA completed");
}
}
class ThreadB extends Thread
{
private ThreadA threadA;
ThreadB(ThreadA threadA)
{
this.threadA = threadA;
}
public void run()
{
System.out.println("ThreadB is running");
try {
System.out.println("ThreadB is sleeping for 5 seconds");
Thread.sleep(5000);
System.out.println("ThreadB wakes up from sleep and interrupts ThreadA ");
threadA.interrupt();
} catch (InterruptedException e) {
System.out.println("Sleep of ThreadB get interrupted");
}
System.out.println("ThreadB completed");
}
}

 

The main thread creates and starts two thread objects known as threadA and threadB.  threadA will be slept for 15 seconds while threadB is slept for 5 seconds. Therefore the threadB is guaranteed to be wake up before threadA wakes up.  Once the threadB wakes up, it interrupts the threadA which is still sleeping. Then the sleeping threadA will get interrupted and  it throws the InterruptedException. The thrown exception will be caught by the catch block and the threadA will be resumed from the catch block.

If you run the above sample program, you will get the following output.

main thread is started
main thread completed
ThreadB is running
ThreadB is sleeping for 5 seconds
ThreadA is running
ThreadA is sleeping for 15 seconds 
ThreadB wakes up from sleep and interrupts ThreadA 
ThreadB completed
Sleep of ThreadA get interrupted 
ThreadA completed

Search
Related Articles

Leave a Comment: