Thread sleep() method in Java

In this post, we will see Thread sleep() method in java. Let’s see some points related to the sleep() method.

  • Method declaration – There are two overloaded versions has been defined for sleep() method and both throws checked exception i.e InterruptedException while using sleep() method we need to use try/catch or throws keyword.

public static native void sleep(long millisec) throws InterruptedException.
public static void sleep(long millisec, int nanosec) throws InterruptedException.

  • What is does – It allows currently executing thread to sleep for a specific amount of time(the time we pass as argument). The time will be in milliseconds.
  • When to use – If a thread doesn’t want to perform any task for a specific time then we can go for sleep() method.
  • We can’t notify a thread while sleeping time.

Let’s see an example where we will not use sleep() method.

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");
		}

	}
}

public class ThreadExample1 {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 myThread1 = new MyThread1();
		myThread1.start();
		for (int i = 0; i < 5; i++) {
			System.out.println("main thread");
		}

	}
}

Output –

main thread
main thread
main thread
main thread
main thread
thread1
thread1
thread1
thread1
thread1

Note – Since both threads will execute simultaneously, the output may vary. It depends on Thread scheduler, which thread will get picked first. For getting the different output you need to execute the program multiple time.

another possible output –

thread1
thread1
thread1
thread1
thread1
main thread
main thread
main thread
main thread

Example2 – In this example, we are going to use sleep() method.

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");
		}

	}
}


public class ThreadSleepExample {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 thread1 = new MyThread1();
		thread1.start();
		Thread.sleep(1000);
		for (int i = 0; i < 5; i++) {
			System.out.println("main thread");
		}
	}
}

Output is –

thread1
thread1
thread1
thread1
thread1
main thread
main thread
main thread
main thread
main thread

In the above program, at line 16 we are calling start() method. The start() method will call run() method and yes we have “thread1” first as output. Since we are calling Thread.sleep() method at line 17, then the current thread i.e main thread will sleep for one second and after one-second main thread will start.  In this case, the output will be consistent.

In the above program inside run() method we are executing for loop only five time, what will happen if we execute for loop for 500 times or more times and reduce the sleep time to 2 milliseconds. Would output still consistent?

It may not, because main thread will sleep for 2 milliseconds and the work we ask to perform thread1 may not complete in 2 milliseconds(i.e looping 500 times may take more time than 2 milliseconds). In this case, main thread execution starts at the same time and output would not be consistent.

Let’s see below example.

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 500; i++) {
			System.out.println("thread1");
		}
 
	}
}
 
 
public class ThreadSleepExample {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 thread1 = new MyThread1();
		thread1.start();
		Thread.sleep(2);//we are saying to main thread for 2 second
		for (int i = 0; i < 5; i++) {
			System.out.println("main thread");
		}
		
	}
}

In output we can see “main thread” has been executed simultaneously.

Note – Don’t get confused, in above example Thread.sleep() will get called on the main thread.

Let’s see another example, where we will call sleep() method on thread1. The thread1 will wait for one sec meanwhile main thread will get executed.

Example – 3

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");

		}

	}
}

public class ThreadSleepExample {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 thread1 = new MyThread1();
		thread1.sleep(1000);
		thread1.start();

		for (int i = 0; i < 5; i++) {
			System.out.println("main thread");
		}
	}
}

Output is –

main thread
main thread
main thread
main thread
main thread
thread1
thread1
thread1
thread1
thread1

Example 4 – In this example, we will two user define threads thread1 and thread2. We will call Thread.sleep() on the main thread.

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");
		}
 
	}
}
 
class MyThread2 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread2");
		}
 
	}
}
 
public class ThreadSleepExample {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 thread1 = new MyThread1();
		MyThread2 thread2 = new MyThread2();
		thread1.start();
		Thread.sleep(1000);//we are saying to main thread, sleep for 1 second
		thread2.start();
		
	}
}

The output is consistent as below-

thread1
thread1
thread1
thread1
thread1
thread2
thread2
thread2
thread2
thread2

Example 5 – In this example, we will two user define threads thread1 and thread2. We will call Thread.sleep() on thread1 .

That’s all about Thread sleep() method in Java.

Thread sleep() method docs.