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

  • 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);
}
}
}
0
1
2
3
4
  • A thread can interrupt a sleeping thread or waiting thread by using interrupt() method of thread class.
/* Interrupts this thread. */
public void interrupt()
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();
}
}
}
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();
}
}
}
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)

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Laravel: Accessing Arrays & Objects

sending notification to your user by your phone

Serverless Telegram bot on AWS Lambda

Code Analysis using SonarQube — Laravel Project

A scalable approach to persistent operations toggles in a NodeJS / Express app

Kubernetes for Data

API Gateway

Sitecore Unit Testing Guide — Part 2 : Using Fixtures

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Single And Multi Dimensional Array in Java

Java Design Patterns: Builder

Top Reasons Why Java Development is still Relevant in 2022

Why Java is Relevant in 2022

TIL 0510 Java Lambda