Thread.sleep() in Java with Examples: Halting Thread Execution

Learn how to use the Thread.sleep() method in Java to pause thread execution. Explore both variants of sleep() with examples, understanding how they help control thread flow by pausing for a specified duration before resuming operation.



Thread.sleep() in Java with Examples

The Thread class in Java provides two variants of the sleep() method. One variant accepts a single argument, while the other accepts two arguments. The sleep() method is used to halt the execution of a thread for a specified amount of time. After the sleeping time is over, the thread resumes execution from where it left off.

The sleep() Method Syntax

The syntax of the sleep() method is as follows:

Syntax

public static void sleep(long mls) throws InterruptedException
public static void sleep(long mls, int n) throws InterruptedException

The method sleep() with one parameter is a native method, implemented in another programming language. The variant with two parameters is not a native method and is implemented in Java. Both methods throw a checked InterruptedException, so they require a try-catch block or the throws keyword.

Parameters

  • mls: The time in milliseconds for which the thread will sleep.
  • n: Additional time for the thread to sleep. The range of n is from 0 to 999999.

Important Points to Remember

  • The Thread.sleep() method halts the execution of the current thread.
  • If another thread interrupts the sleeping thread, an InterruptedException is thrown.
  • If the system executing the threads is busy, the actual sleeping time may be longer than specified.

Example: sleep() Method on a Custom Thread

In this example, two threads are created, and each sleeps for 500 milliseconds between iterations of a loop.

Code Example

class TestSleepMethod1 extends Thread {
public void run() {
    for (int i = 1; i < 5; i++) {
        try { Thread.sleep(500); } catch (InterruptedException e) { System.out.println(e); }
        System.out.println(i);
    }
}

public static void main(String args[]) {
    TestSleepMethod1 t1 = new TestSleepMethod1();
    TestSleepMethod1 t2 = new TestSleepMethod1();
    t1.start();
    t2.start();
}
}
Output

1
1
2
2
3
3
4
4

Example: sleep() Method on the Main Thread

This example demonstrates using the sleep() method in the main thread, where the thread sleeps for 1000 milliseconds (1 second) between each iteration.

Code Example

import java.lang.Thread;
import java.io.*;

public class TestSleepMethod2 {
public static void main(String argvs[]) {
    try {
        for (int j = 0; j < 5; j++) {
            Thread.sleep(1000);
            System.out.println(j);
        }
    } catch (Exception expn) {
        System.out.println(expn);
    }
}
}
Output

0
1
2
3
4

Example: sleep() Method with Negative Time

In this example, we attempt to use the sleep() method with a negative time value, which will result in an IllegalArgumentException.

Code Example

import java.lang.Thread;
import java.io.*;

public class TestSleepMethod3 {
public static void main(String argvs[]) {
    try {
        for (int j = 0; j < 5; j++) {
            Thread.sleep(-100);
            System.out.println(j);
        }
    } catch (Exception expn) {
        System.out.println(expn);
    }
}
}
Output

java.lang.IllegalArgumentException: timeout value is negative