Java Programming Tutorials

Java programming tutorials with many code examples!

Java Thread pause

Problem:

How to pause Java Threads? In this post we are going to show three ways to do that – using Thread class and TimeUnit from java.util.concurrent. Read on!

Solution:

In the below example we’ve create three methods that pause current thread using three different methods:

  • static void Thread.sleep(long millis)
    Pauses the current thread for given number of milliseconds.
  • static void Thread.sleep(long millis, int nanos)
    Pauses the current thread for given number of milliseconds plus nanoseconds.
  • void TimeUnit.sleep(long timeout)
    Calls Thread.sleep using this time unit.

Times specified in to each of these methods depend on accuracy of system timers and schedulers and are not guaranteed. It can be seen in the results below, where we print expected sleep time and elapsed time – there can be small differences.

package com.farenda.java.lang;

import java.util.concurrent.TimeUnit;

public class ThreadPauseExample {

    public static void main(String[] args) {
        pauseUsingThreadSleep();

        pauseUsingThreadSleepWithNanos();

        pauseUsingTimeUnit();
    }

    private static void pauseUsingThreadSleep() {
        System.out.print("Pausing Thread.sleep(100)... ");
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // someone interrupted me!
        }
        long end = System.currentTimeMillis();
        System.out.printf("done after: %d ms%n", end - start);
    }

    private static void pauseUsingThreadSleepWithNanos() {
        System.out.print("Pausing Thread.sleep(0, 500000)... ");
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(0, 500000);
        } catch (InterruptedException e) {
            // someone interrupted me!
        }
        long end = System.currentTimeMillis();
        System.out.printf("done after: %d ms%n", end - start);
    }

    private static void pauseUsingTimeUnit() {
        System.out.print("Pausing TimeUnit.MILLISECONDS.sleep(100)... ");
        long start = System.currentTimeMillis();
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            // someone interrupted me!
        }
        long end = System.currentTimeMillis();
        System.out.printf("done after: %d ms%n", end - start);
    }
}

As you can see the code using java.util.concurrent.TimeUnit is the most readable one. It’s immediately clear whether the value is milliseconds, seconds or other time unit. TimeUnit was great addition to Java 5.

And here’s the result of the above code:

Pausing Thread.sleep(100)... done after: 100 ms
Pausing Thread.sleep(0, 500000)... done after: 1 ms
Pausing TimeUnit.MILLISECONDS.sleep(100)... done after: 101 ms

Thread pause using nanos is usually useless and cannot be relied upon. At least on our machine. :-)

There is also Thread.suspend() method, but it has been deprecated and is deadlock-prone, so we won’t describe it.

Share with the World!