MultiThreaded Programming  «Prev  Next»

Lesson 6 Sleeping threads
Objective Pause work safely with Thread.sleep, handle interrupts correctly, and choose modern scheduling alternatives for periodic tasks.

Sleeping Threads in Java

Calling start() on a Thread begins execution of its run() method on a new thread of control. (Calling run() directly runs on the current thread.)
You can temporarily pause a thread with Thread.sleep(n), where n is in milliseconds. After the sleep completes, or the thread is interrupted, the thread continues from the next statement. Sleeping is often used for simple delays and backoff, but it is not a precise timer.

Using sleep correctly


import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

class Worker implements Runnable {
    private final AtomicBoolean running = new AtomicBoolean(true);

    public void stop() { running.set(false); }

    @Override public void run() {
        while (running.get()) {
            // 1) Do periodic work
            doWork();

            // 2) Delay ~1 second (not real-time precise)
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException ie) {
                // Best practice: restore interrupt status, then exit or propagate
                Thread.currentThread().interrupt();
                break;
            }
        }
    }

    private void doWork() {
        // ... your task ...
    }
}


Key points:

Modern alternative: schedule at a fixed rate

Prefer a scheduler for periodic work such as UI refresh, polling, or maintenance tasks.


import java.util.concurrent.*;

public class SchedulerDemo {
    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();

        ScheduledFuture<?> handle = ses.scheduleAtFixedRate(
            () -> updateFrame(), 0, 1, TimeUnit.SECONDS);

        // Let it run for 10 seconds, then stop
        TimeUnit.SECONDS.sleep(10);
        handle.cancel(true);   // may interrupt the running task
        ses.shutdown();
    }

    static void updateFrame() {
        // periodic task body
    }
}

Why this is better than sleep in a loop: The scheduler compensates for drift (with scheduleAtFixedRate), handles cancellation, and centralizes thread management.

Common pitfalls


Self-check

  1. What should you do when a sleeping thread is interrupted?
  2. When would you choose ScheduledExecutorService over Thread.sleep?
  3. Does Thread.sleep release any monitors held by the thread?



Exercise: Modify Slide Show

Update the slideshow application to use either:
  1. a scheduler with scheduleAtFixedRate, or
  2. a worker thread with proper interrupt handling (as shown above).
Compare drift and responsiveness under load.
Slide Show – Exercise
Note on exceptions: An exception represents an unusual or error condition that disrupts normal control flow. You’ll explore exception design and handling patterns in the next module.

SEMrush Software