Skip to content

Instantly share code, notes, and snippets.

@gchiam
Created October 18, 2020 10:28
Show Gist options
  • Save gchiam/bf410798e354e933a16cd0111a798d3c to your computer and use it in GitHub Desktop.
Save gchiam/bf410798e354e933a16cd0111a798d3c to your computer and use it in GitHub Desktop.
import java.util.concurrent.TimeUnit;
/**
* Simple Java Program to show how to execute threads in a particular order. You
* can enforce ordering or execution sequence using Thread.join() method in
* Java. * * @author Javin Paul
*/
public class JoinDemo {
private static class ParallelTask implements Runnable {
private Thread predecessor;
@Override public void run() {
System.out.println(Thread.currentThread().getName() + " Started");
if (predecessor != null) {
try {
predecessor.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " Finished");
}
public void setPredecessor(Thread t) {
this.predecessor = t;
}
}
public static void main(String[] args) {
// we have three threads and we need to run in the
// order T1, T2 and T3 i.e. T1 should start first
// and T3 should start last.
// You can enforce this ordering using join() method
// but join method must be called from run() method
// because the thread which will execute run() method
// will wait for thread on which join is called.
ParallelTask task1 = new ParallelTask();
ParallelTask task2 = new ParallelTask();
ParallelTask task3 = new ParallelTask();
final Thread t1 = new Thread(task1, "Thread - 1");
final Thread t2 = new Thread(task2, "Thread - 2");
final Thread t3 = new Thread(task3, "Thread - 3");
task2.setPredecessor(t1);
task3.setPredecessor(t2);
// now, let's start all three threads
t1.start();
t2.start();
t3.start();
}
}
/**
Output
Thread - 1 Started
Thread - 1 Finished
Thread - 2 Started
Thread - 3 Started
Thread - 2 Finished
Thread - 3 Finished
Read more: https://www.java67.com/2015/07/how-to-join-two-threads-in-java-example.html#ixzz6bDWHDPHp
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment