Sunday, March 19, 2017

java thread join example

package threadjoinexample;

import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadJoinExample {

    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable(), "1st");
        Thread t2 = new Thread(new MyRunnable(), "2nd");
        Thread t3 = new Thread(new MyRunnable(), "3rd");

//        try {
//            t2.join();
//        } catch (InterruptedException ex) {
//            Logger.getLogger(ThreadJoinExample.class.getName()).log(Level.SEVERE, null, ex);
//        }

        t1.start();

        //start second thread after waiting for 2 seconds or if it's dead
//        try {
////            t1.join(2000);
//            System.out.println("1st try");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadJoinExample.class.getName()).log(Level.SEVERE, null, ex);
        }
        t2.start();
       
       
            //checks if this thread is alive
      System.out.println(t1.getName()+"=, isAlive = " + t1.isAlive());

        //start third thread only when first thread is dead
//        try {
////            t1.join();
//            System.out.println("2nd block  try="+t1.getName());
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        try {
            t2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadJoinExample.class.getName()).log(Level.SEVERE, null, ex);
        }
        t3.start();

        //let all threads finish execution before finishing main thread
        try {
           
           
            t1.join();
           
            System.out.print(t2.getName());
            //checks if this thread is alive
      System.out.println(", t2 isAlive = " + t2.isAlive());
           
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("All threads are dead, exiting main thread");
    }

}

class MyRunnable implements Runnable {

    @Override
    public synchronized void run() {
        System.out.println("--Thread started:::" + Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
            System.out.println("run() 4s after=" + Thread.currentThread().getName());

            for (int i = 1; i <= 5; i++) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    System.out.println(e);
                }
                System.out.println(i + " " + Thread.currentThread().getName());
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("--Thread ended:::" + Thread.currentThread().getName());
    }

}








----------------------------------
output:
--Thread started:::1st
run() 4s after=1st
1 1st
2 1st
3 1st
4 1st
5 1st
--Thread ended:::1st
1st=, isAlive = false
--Thread started:::2nd
run() 4s after=2nd
1 2nd
2 2nd
3 2nd
4 2nd
5 2nd
--Thread ended:::2nd
2nd, t2 isAlive = false
--Thread started:::3rd
run() 4s after=3rd
1 3rd
2 3rd
3 3rd
4 3rd
5 3rd
--Thread ended:::3rd
All threads are dead, exiting main thread

No comments:

Post a Comment