java tutorial - Java thread join - java programming - learn java - java basics - java for beginners



Java thread join

Learn Java - Java tutorial - Java thread join - Java examples - Java programs

The join() method

  • The join() methods waits until the thread will die.
  • In other word, it will cause the presently running threads to end its executing until the thread joins to complete its task.

Syntax:

public void join()throws InterruptedException
public void join(long milliseconds)throws InterruptedException
click below button to copy the code. By - java tutorial - team

join() method

public class JoinMethod extends Thread{
 public void run(){
  for(int i=0;i<=4;i++){
   try{
    Thread.sleep(100);
   }catch(Exception e){System.out.println(e);}
  System.out.println(i);
  }
 }
public static void main(String args[]){
 JoinMethod time1=new JoinMethod();
 JoinMethod time2=new JoinMethod();
 JoinMethod time3=new JoinMethod();
 time1.start();
 try{
  time1.join();
 }catch(Exception e){System.out.println(e);}

 time2.start();
 time3.start();
 }
}
click below button to copy the code. By - java tutorial - team

Output

0
1
2
3
4
0
0
1
1
2
2
3
3
4
4
  • When time1 is completes its task then time2 and time3 starts executing.

join(long miliseconds) method

public class JoinMethod extends Thread{  
 public void run(){  
  for(int i=2;i<=7;i++){  
   try{  
    Thread.sleep(700);  
   }catch(Exception e){System.out.println(e);}  
  System.out.println(i);  
  }  
 }  
public static void main(String args[]){  
 JoinMethod time1=new JoinMethod();  
 JoinMethod time2=new JoinMethod();  
 JoinMethod time3=new JoinMethod();  
 time1.start();  
 try{  
  time1.join(1700);  
 }catch(Exception e){System.out.println(e);}  
  
 time2.start();  
 time3.start();  
 }  
}  
click below button to copy the code. By - java tutorial - team

Output

2
3
4
2
2
5
3
3
6
4
4
7
5
5
6
6
7
7
  • When time1 is completes its task for 1700 milliseconds (3 times) then time2 and time3 starts executing.

getName(),setName(String) and getId() method:

public String getName()
public void setName(String name)
public long getId()
click below button to copy the code. By - java tutorial - team
public class JoinMethod extends Thread{  
  public void run(){  
   System.out.println("running...");  
  }  
 public static void main(String args[]){  
  JoinMethod time1=new JoinMethod();  
  JoinMethod time2=new JoinMethod();  
  System.out.println("Name of t1:"+time1.getName());  
  System.out.println("Name of t2:"+time2.getName());  
  System.out.println("id of t1:"+time1.getId());  
  
  time1.start();  
  time2.start();  
  
  time1.setName("Welcome to wikitechy");  
  System.out.println("After changing name of t1:"+time1.getName());  
 }  
}  
click below button to copy the code. By - java tutorial - team
Name of t1:Thread-0
Name of t2:Thread-1
id of t1:21
After changing name of t1:Welcome to wikitechy
running...
running...

The currentThread() method:

  • These method returns a reference to the currently executing thread object.

Syntax:

public static Thread currentThread()
click below button to copy the code. By - java tutorial - team

currentThread() method

public class JoinMethod extends Thread{  
 public void run(){  
  System.out.println(Thread.currentThread().getName());  
 }  
  
 public static void main(String args[]){  
  JoinMethod time1=new JoinMethod();  
  JoinMethod time2=new JoinMethod();  
  
  time1.start();  
  time2.start();  
 }  
}  
click below button to copy the code. By - java tutorial - team

Output

Thread-0
Thread-1

Related Searches to Java thread join