• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Java | Thread Class Methods: sleep, join
  1. The sleep Method
  2. The join Method

  1. The sleep Method
    Calling the sleep method causes the current thread to pause its execution and move to the "TIMED_WAITING" state (not "Waiting").

    The thread will be put to sleep for the duration specified in the method's argument (in milliseconds). When this time elapses, the JVM changes the thread's state to "RUNNABLE", making it eligible for execution again.

    The sleep method is a static method, so it is not necessary to have a reference to a thread instance to call it. It always operates on the currently executing thread.

    Note: The sleep method can throw an InterruptedException, which must be handled or declared.
    public class MainClass {
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : START");
    
            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println(
                            Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : START");
                    try {
                        Thread.sleep(1000); // Sleep for 1000 milliseconds (1 second)
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt(); // Restore interrupted status
                        e.printStackTrace();
                    }
                    System.out.println(
                            Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : END");
                }
            };
    
            Thread myThread1 = new Thread(myRunnable, "mySleepThread");
    
            myThread1.start();
    
            System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : END");
        }
    }
    Output:
    main (1) : START
    main (1) : END
    mySleepThread (19) : START
    mySleepThread (19) : END
    Notes:
    • The main thread completes before the sleep thread finishes, demonstrating concurrent execution.
    • The sleep thread pauses for 1 second before printing its END message.
    • Thread IDs may vary between different program runs.
  2. The join Method
    Calling the join method causes the current thread to pause its execution and move to the "WAITING" state.

    The current thread will wait until the thread (whose instance was used to call the join method) finishes its execution before it can be scheduled again by the JVM to resume its execution.

    The join method is an instance method, so it is necessary to have a reference to a thread instance in order to call it.
    public class MainClass {
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : START");
    
            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println(
                            Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : START");
                    try {
                        Thread.sleep(1000); // Simulate some work
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        e.printStackTrace();
                    }
                    System.out.println(
                            Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : END");
                }
            };
    
            Thread myThread1 = new Thread(myRunnable, "myJoinThread");
    
            myThread1.start();
    
            try {
                myThread1.join(); // Main thread waits for myThread1 to complete
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
    
            System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : END");
        }
    }
    Output:
    main (1) : START
    myJoinThread (19) : START
    myJoinThread (19) : END
    main (1) : END
    In this example, two important points should be noted:
    • The myThread1.join(); statement is executed by the "main" thread.
    • The reference used to invoke the join method belongs to the "myJoinThread" thread.

    As you can see from the program output, the "main" thread continued its execution only after the "myJoinThread" thread completed its execution.

    Joining an Already Terminated Thread:
    If the current thread "main" tries to join a thread "myJoinThread" that has already completed its execution, the JVM recognizes that the "myJoinThread" thread has already terminated, and therefore allows the "main" thread to continue its execution without waiting.
    public class MainClass {
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : START");
    
            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println(
                            Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : EXECUTING");
                }
            };
    
            Thread myThread1 = new Thread(myRunnable, "myJoinThread");
    
            myThread1.start();
    
            try {
                Thread.sleep(1000); // Give enough time for myThread1 to complete
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
    
            System.out.println("myJoinThread state : " + myThread1.getState());
    
            try {
                myThread1.join(); // This will return immediately since thread is already terminated
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
    
            System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().threadId() + ") : END");
        }
    }
    Output:
    main (1) : START
    myJoinThread (19) : EXECUTING
    myJoinThread state : TERMINATED
    main (1) : END
© 2025  mtitek