Introduction
In this article we discuss Joining and Naming of threads.
Thread join() method
This method waits for a thread to die. In other words, we can say it causes the currently running thread to stop executing until the thread it joins completes its task.
Syntax of join() public method
void join()
void join (long mili-seconds)
Let's see an example
In this example, when th1 completes its task th2 and th3 start executing.
MultithreadEx.java
- class MultithreadEx extends Thread {
- public void run() {
- for (int x = 1; x <= 6; x++) {
- try {
- Thread.sleep(400);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- System.out.println(x);
- }
- }
- public static void main(String args[]) {
- MultithreadEx th1 = new MultithreadEx();
- MultithreadEx th2 = new MultithreadEx();
- MultithreadEx th3 = new MultithreadEx();
- th1.start();
- try {
- th1.join();
- } catch (Exception ex) {
- System.out.println(ex);
- }
- th2.start();
- th3.start();
- }
- }
Output

2. Example
In this example; we use the join(long miliseconds) method. So, when th1 completes the task for 2000 miliseconds (5 times) then th2 and th3 start executing.
MultithreadEx1.java
- class MultithreadEx1 extends Thread {
- public void run() {
- for (int x = 1; x <= 6; x++) {
- try {
- Thread.sleep(400);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- System.out.println(x);
- }
- }
- public static void main(String args[]) {
- MultithreadEx1 th1 = new MultithreadEx1();
- MultithreadEx1 th2 = new MultithreadEx1();
- MultithreadEx1 th3 = new MultithreadEx1();
- th1.start();
- try {
- th1.join(2000);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- th2.start();
- th3.start();
- }
- }




Join the conversation! Your thoughts help the community grow.