Thursday, 25 August 2016

How to ensure that the Parent thread will die after the Child thread?

In below given example, the Parent thread dies before the Child thread.
classChildThread1 extends Thread {
     @Override
     public void run() {
           try {
                System.out.println("Child thread start");
                Thread th = Thread.currentThread();
               
                if("Child Thread".equals(th.getName())) {
                     Thread.sleep(1000);
                }
               
                System.out.println("Child thread closed");
               
           } catch(Exception e) {
                System.out.println("exception is" + e);
           }
     }
}

public classCloseChildThreadTest {
    public static void main(String args[]) throws InterruptedException {
    System.out.println("Main thread start");
    ChildThread1 cThread = new ChildThread1();
   
        cThread.setName("Child Thread");
        cThread.start();
        System.out.println("Main thread closed");
    }
}
Output:
Main thread start
Main thread closed
Child thread start
Child thread closed

Ensure the parent dies after the Child thread?

1. Using join() method
classChildThread1 extends Thread {
     @Override
     public void run() {
           try {
                System.out.println("Child thread start");
                Thread th = Thread.currentThread();
               
                if("Child Thread".equals(th.getName())) {
                     Thread.sleep(1000);
                }
               
                System.out.println("Child thread closed");
               
           } catch(Exception e) {
                System.out.println("exception is" + e);
           }
     }
}

public classCloseChildThreadTest {
    public static void main(String args[]) throws InterruptedException {
    System.out.println("Main thread start");
    ChildThread1 cThread = new ChildThread1();
   
        cThread.setName("Child Thread");
        cThread.start();
       
        cThread.join();
        System.out.println("Main thread closed");
    }
}
Output:
Main thread start
Child thread start
Child thread closed
Main thread closed

Using CountDownLatch:
package com.thread;

importjava.util.concurrent.CountDownLatch;
classChildThread1 extends Thread {
     CountDownLatch latch;
     publicChildThread1(CountDownLatch latch) {
           this.latch = latch;
     }

     @Override
     public void run() {
           try {
                System.out.println("Child thread start");
                Thread th = Thread.currentThread();
               
                if("Child Thread".equals(th.getName())) {
                     Thread.sleep(1000);
                }
                System.out.println("Child thread closed");
                /* Count down when the run method execution finished. */
                latch.countDown();
           } catch(Exception e) {
                System.out.println("exception is" + e);
           }
     }
}

public classCloseChildThreadTest {
    public static void main(String args[]) throws InterruptedException {
        /*Set count 1 to CountDownLatch. */
        CountDownLatch latch = new CountDownLatch(1);
        System.out.println("Main thread start");
        ChildThread1 cThread = new ChildThread1(latch);

        cThread.setName("Child Thread");
        cThread.start();

        /* Await till the end of the Child thread. */
        latch.await();
        System.out.println("Main thread closed");
    }
}
Output:
Main thread start
Child thread start
Child thread closed
Main thread closed

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...