Today, i walk you through Java Thread sleep and join methods to know how it works and how to use in practical terms. Lets begins
Java Thread Sleep
How Sleep method works
Thread.sleep() works with the thread scheduler to put the current thread in wait state for specified period of time. Thread state is changed to runnable state as the wait time is over and wait for the CPU for further execution.
Thread Sleep Example
Here is a program where Thread.sleep() is used to pause the current thread execution for 5 seconds.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javabycode.multithreads; public class ThreadSleepExample { public static void main(String[] args) throws InterruptedException { long startTime = System.currentTimeMillis(); Thread.sleep(5000); System.out.println("Sleep time in millisecond = "+(System.currentTimeMillis()-startTime)); } } |
Important points
a. Thread sleep always pause the current thread execution.
b. If the thread is run in a quiet system, the actual time for sleep is near to the specified sleep tim. But for a busy system it will be little bit more.
c. Thread sleep doesn’t lose any monitors or locks current thread.
d. The InterruptedException is thrown if any other thread interrupt the sleep thread.
Java Thread Join
How Join method works
Thread join() method allows one thread to wait until another thread completes its execution
Thread Join Example
Here is a simple example showing usage of Thread join methods. This program shows that our threads finish before the main thread finish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.javabycode.multithreads; public class ThreadJoinDemo { public static void main(String[] args) { Runnable r1 = () -> { System.out.println("Thread 1 is running"); // do stuffs System.out.println("Thread 1 is ending"); }; Thread t1 = new Thread(r1, "t1"); Runnable r2 = () -> { System.out.println("Thread 2 is running"); }; Thread t2 = new Thread(r2, "t2"); Runnable r3 = () -> { System.out.println("Thread 3 is running"); }; Thread t3 = new Thread(r3, "t3"); t1.start(); try { t1.join(); //pause the current thread until thread t1 finish execution } catch (InterruptedException e) { e.printStackTrace(); } t2.start(); t3.start(); //let all threads finish before finishing main thread try { t1.join(); t2.join(); t3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("All threads finish before finishing main thread"); } } |
Output of the above program is
Thread join important points
public final void join(): This method puts the current thread on wait until the joined thread finish.
public final synchronized void join(long millis): This method is used to wait for specified milliseconds.
public final synchronized void join(long millis, int nanos): This method is used to wait for thread to finish for given milliseconds plus nanoseconds.
What is the difference between join() and sleep() method in Java?
a. The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
b. The sleep(long millis) method causes the currently executing thread to sleep for the specified number of milliseconds.
That’s all about Java Thread sleep and join. Happy learning.
References
Pausing Execution with Sleep
Joins
Download the complete source code, click link below
JavaMultithreadExample.zip (208 downloads)