Friday, 26 August 2016

Thread join

Join() method implementation

This method is use to join one thread at the end of another thread which means the join method waits for the currently running thread to stop executing until the thread it joins with complete the tasks
Syntax :
Try{
<Thread_object> .join()
}catch(InterruptedException e){}

Example:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multithreadingtc;

/**
 *
 * @author shashank
 */

class Multithreading implements Runnable
{
     public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println("run by" + Thread.currentThread().getName() + " remainder is i is " +i%10);
          
               Thread.yield();
           
        }
    }
}
public class MultithreadingTC{

    public static void main(String[] args) {
        // TODO code application logic here
        Multithreading th = new Multithreading();
        Thread t = new Thread(th);
        t.setName(" first");
        t.start();
        try
        {
            t.join();
        }catch(InterruptedException ex){}
        Thread t1 = new Thread(th);
        t1.setName(" second");
        t1.start();
      
    }
   
}




No comments:

Post a Comment