Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • How to use Thread Join() Method ?

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 392
    Comment on it

    Hello Readers , In this blog I am going to discuss about Join() Method and how we can use ?

    As we know that Thread execution In Java occurs according to Thread Priority.The Threads having same priority  can be executed randomly. However we can make Thread to wait until one thread complete its execution by the help of Join() method. Join () method is a type of checked exception so it should be surround with try and catch block otherwise it throws InterruptedException.

    Lets see through Example :

    1. Creating class A

    public class A extends Thread{
    
    	public void run()
    {
    	for(int i=1;i<10;i++)
    	{
    		System.out.println("calling from A "+i);
    	}
    }
    }

    2. Creating class B

    public class B extends Thread{
    
    		public void run()
    	{
    		for(int i=1;i<10;i++)
    		{
    			System.out.println(" calling from B :  "+i);
    		}
    	}
    
    }
    

    If We don't use Join() Method , Lets see what's result :

    public class Demo {
     public static void main(String args[])
     {
    	 A x=new A();
    	 B y=new B();
    	 x.start();
    	 y.start();
     }
    }

    Output :

     calling from B :  1
    calling from A 1
     calling from B :  2
    calling from A 2
     calling from B :  3
    calling from A 3
     calling from B :  4
    calling from A 4
     calling from B :  5
     calling from B :  6
    calling from A 5
     calling from B :  7
    calling from A 6
     calling from B :  8
    calling from A 7
     calling from B :  9
    calling from A 8
    calling from A 9

    Now lets see the Result on using Join() Method :

    public class Demo {
     public static void main(String args[])
     {
    	 A x=new A();
    	 B y=new B();
    	 x.start();
    	try {
    		x.join();
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    	}
    	 y.start();
     }
    }
    

    Output:

    calling from A 1
    calling from A 2
    calling from A 3
    calling from A 4
    calling from A 5
    calling from A 6
    calling from A 7
    calling from A 8
    calling from A 9
     calling from B :  1
     calling from B :  2
     calling from B :  3
     calling from B :  4
     calling from B :  5
     calling from B :  6
     calling from B :  7
     calling from B :  8
     calling from B :  9
    

     

    join() method

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: