In this example we will show how to use thread.join in Java.
Source Code
package com.beginner.examples;
public class ThreadJoinExample extends Thread{
Thread thread1;
public ThreadJoinExample(Thread thread1){
this.thread1 = thread1;
}
@Override
public void run() {
try
{
thread1.join();
// print current thread name
System.out.println(Thread.currentThread().getName());
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread thread1 = Thread.currentThread();
for(int i = 0; i < 3; i++){
ThreadJoinExample thread = new ThreadJoinExample(thread1);
thread.start();
}
}
}
Output:
Thread-0
Thread-1
Thread-2