How do I use the sleep method of the Thread class?

Here is another example that use the Thread.sleep() method. In the example we create two instances of the ThreadSleepAnotherDemo, we give each thread a name and the sleep interval so that we can see how to thread execution.

package org.kodejava.lang;

import java.util.Calendar;

public class ThreadSleep implements Runnable {
    private final String threadName;
    private final long sleep;

    public ThreadSleep(String threadName, long sleep) {
        this.threadName = threadName;
        this.sleep = sleep;
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new ThreadSleep("FirstThread", 1000));
        Thread thread2 = new Thread(new ThreadSleep("SecondThread", 3000));

        // Start the threads
        thread1.start();
        thread2.start();
    }

    // The run() method will be invoked when the thread is started.
    public void run() {
        System.out.println("Start thread [" + this.threadName + "]");
        try {
            while (true) {
                // Pause the thread for "sleep" milliseconds.
                Thread.sleep(this.sleep);
                System.out.println("[" + threadName + "]" +
                        Calendar.getInstance().getTime());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finish thread [" + this.threadName + "]");
    }
}

These are the sample output produced by the code snippet above:

Start thread [FirstThread]
Start thread [SecondThread]
[FirstThread]Mon Oct 25 11:52:13 CST 2021
[FirstThread]Mon Oct 25 11:52:14 CST 2021
[SecondThread]Mon Oct 25 11:52:15 CST 2021
[FirstThread]Mon Oct 25 11:52:15 CST 2021
[FirstThread]Mon Oct 25 11:52:16 CST 2021

How do I pause the current thread?

You can pause a current thread for a number of milliseconds by using the sleep() method of the Thread class. While the current thread is sleeping, it will allow other threads to execute.

package org.kodejava.lang;

public class ThreadSleepDemo implements Runnable {
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadSleepDemo());
        thread.start();
    }

    // The run() method will be invoked when the thread is started.
    public void run() {
        System.out.println("Start..");
        try {
            // Wait for 10 seconds
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finish...");
    }
}

How do I add a delay in my program?

You have a problem that you want to add a delay for a couple of seconds in your program. Using Thread.sleep() method we can add delay in our application in a millisecond time. The Thread.sleep() need to be executed inside a try-catch block, and we need to catch the InterruptedException. Let’s see the code snippet below.

package org.kodejava.lang;

public class DelayExample {
    public static void main(String[] args) {
        // This program demonstrate how to add a delay in our 
        // application.
        for (int i = 0; i < 10; i++) {
            // Print the value of i
            System.out.println("i = " + i);

            try {
                // Using Thread.sleep() we can add delay in our 
                // application in a millisecond time. For the example 
                // below the program will take a deep breath for one 
                // second before continue to print the next value of 
                // the loop.
                Thread.sleep(1000);

                // The Thread.sleep() need to be executed inside a 
                // try-catch block and we need to catch the 
                // InterruptedException.
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }
}