Java — Java.lang.Thread.sleep() Method | Code Factory

Code Factory
3 min readMay 29, 2020

--

Index Page : Link

Donate : Link

WordPress Blog : Link

  • If a thread don’t want to perform any operation for a particular amount of time then we should go for sleep() method.
/* Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors. */
public static void sleep(long millis) throws InterruptedException
/* Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors. */
public static void sleep(long millis, int nanos) throws InterruptedException
  • Every sleep() method throws InterruptedException, which is checked exception, Hence whenever we are using sleep() method compulsary we should handle InterruptedException either by try catch or throws keyword otherwise we will get CE error.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadSleep {
public static void main(String... args) throws InterruptedException {
for(int i=0; i<5; i++) {
System.out.println(i);
Thread.sleep(1000);
}
}
}

Output :

0
1
2
3
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.
/* Interrupts this thread. */
public void interrupt()

ThreadSleep.java

package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadSleep {
public static void main(String... args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.interrupt(); // #1
System.out.println("Main Thread end");
}
}
class MyThread extends Thread {
public void run() {
try {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Output :

Main Thread end
Child Thread
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.example.thread.MyThread.run(ThreadSleep.java:21)
  • If we comment line #1 then main thread wouldn’t interrupt child thread. In this case child thread will execute 5 times.
  • If we are not commenting line #1 then main thread interrupt child thread.
  • * Whenever we are calling intrrupt() method, If the target thread not in sleeping state or waiting state then there is no any intrrupt() call immediately. Interrupt call will waited until target thread entered into sleeping or waiting state. If the target thread entered into sleeping or waiting state then immediately intrrupt call will interrupt target thread.
  • * If the target thread never entered into sleeping or waiting state in it’s lifetime then there is no impact of intrrupt call. This is the only case where intrrupt call will be wasted.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadSleep {
public static void main(String... args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.interrupt();
System.out.println("Main Thread end");
}
}
class MyThread extends Thread {
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
System.out.println("Entering into sleeping state");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Output :

Main Thread end
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Entering into sleeping state
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.example.thread.MyThread.run(ThreadSleep.java:23)

--

--