Joining Threads in Java

java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join(); it causes the current thread to pause its execution until thread it join completes its execution.If there are multiple threads calling the join() methods that means overloading on join allows the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.There are three overloaded join functions.

  1. join(): It will put the current thread on wait until the thread on which it is called is dead. If thread is interrupted then it will throw InterruptedException.
    Syntax:

    public final void join()

  2. join(long millis) :It will put the current thread on wait until the thread on which it is called is dead or wait for specified time (milliseconds).
    Syntax:

    public final synchronized void join(long millis)

  3. join(long millis, int nanos): It will put the current thread on wait until the thread on which it is called is dead or wait for specified time (milliseconds + nanos).
    Syntax:

    public final synchronized void join(long millis, int nanos)

Example of join() method
class Test extends Thread { public void run() { for(int i=1;i<=5;i++) { try { Thread.sleep(500); }catch(Exception e){System.out.println(e);} System.out.println(i); } } public static void main(String args[]){ Test t1=new Test(); Test t2=new Test(); Test t3=new Test(); t1.start(); try{ t1.join(); }catch(Exception e){System.out.println(e);} t2.start(); t3.start(); } }


Output:
1 2 3 4 5 1 1 2 2 3 3 4 4 5 5

As you can see in the above example,when t1 completes its task then t2 and t3 starts executing.

Example of join(long miliseconds) method
class Test extends Thread{ public void run(){ for(int i=1;i<=5;i++){ try{ Thread.sleep(500); }catch(Exception e){System.out.println(e);} System.out.println(i); } } public static void main(String args[]){ Test t1=new Test(); Test t2=new Test(); Test t3=new Test(); t1.start(); try{ t1.join(1500); }catch(Exception e){System.out.println(e);} t2.start(); t3.start(); } }


Output:
1 2 3 1 1 4 2 2 5 3 3 4 4 5 5

In the above example,when t1 is completes its task for 1500 miliseconds(3 times) then t2 and t3 starts executing.

getName(),setName(String) and getId() method:
class Test extends Thread { public void run() { System.out.println("running..."); } public static void main(String args[]){ Test t1=new Test(); Test t2=new Test(); System.out.println("Name of t1:"+t1.getName()); System.out.println("Name of t2:"+t2.getName()); System.out.println("id of t1:"+t1.getId()); t1.start(); t2.start(); t1.setName("Sonoo Jaiswal"); System.out.println("After changing name of t1:"+t1.getName()); } }


Output:
Name of t1:Thread-0 Name of t2:Thread-1 id of t1:8 After changing name of t1:Sonoo Jaiswal running... running...

The currentThread() method:

The currentThread() method returns a reference to the currently executing thread object.

Syntax:

public static Thread currentThread()

Example of currentThread() method

class Test extends Thread { public void run() { System.out.println(Thread.currentThread().getName()); } } public static void main(String args[]) { Test t1=new Test(); Test t2=new Test(); t1.start(); t2.start(); } }


Output:
Thread-0 Thread-1
// Java program to explain the // concept of joining a thread. import java.io.*; // Creating thread by creating the // objects of that class class ThreadJoining extends Thread { @Override public void run() { for (int i = 0; i < 2; i++) { try { Thread.sleep(500); System.out.println("Current Thread: " + Thread.currentThread().getName()); } catch(Exception ex) { System.out.println("Exception has" + " been caught" + ex); } System.out.println(i); } } } class Test { public static void main (String[] args) { // creating two threads ThreadJoining t1 = new ThreadJoining(); ThreadJoining t2 = new ThreadJoining(); ThreadJoining t3 = new ThreadJoining(); // thread t1 starts t1.start(); // starts second thread after when // first thread t1 is died. try { System.out.println("Current Thread: " + Thread.currentThread().getName()); t1.join(); } catch(Exception ex) { System.out.println("Exception has " + "been caught" + ex); } // t2 starts t2.start(); // starts t3 after when thread t2 is died. try { System.out.println("Current Thread: " + Thread.currentThread().getName()); t2.join(); } catch(Exception ex) { System.out.println("Exception has been" + " caught" + ex); } t3.start(); } }


Output:
Current Thread: main Current Thread: Thread-0 0 Current Thread: Thread-0 1 Current Thread: main Current Thread: Thread-1 0 Current Thread: Thread-1 1 Current Thread: Thread-2 0 Current Thread: Thread-2 1



Instagram