Results 1 to 3 of 3
  1. #1
    adammyth is offline Member
    Join Date
    Dec 2009
    Rep Power

    Exclamation keep child threads running after parent thread dies

    How can i keep child threads running after parent thread dies?

    I tried to use setDaemon method but it didn't work.

    Any idea?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    in a command line application, such as when we launch from the static void main(String[] args), when the main thread exits, that effectively causes the VM to start doing its shutdown..

    Daemon threads are really only good in applets (i think), to allow the thread to run when the [applet] is otherwise finished running, beacuse typically the lifespan of the Java vm in a web browser plugin, is the duration the browser is open, not the duration the user is browsing the page containing the applet.

    so for the command line applications, to cause it to wait until a child thread finishes, we need a piece of busy waiting logic in the main thread

    Java Code:
    public static void main(String[] args) {
     // your thread, of your implementation of runnable
     Thread childThread = new Thread(new Runnable() {
      public void run() {
         for(int i = 0; i < 10; i++) {
          System.out.println("in child thread");
          try { Thread.sleep(1000); } catch (InterruptedException ex) {}
     System.out.println("main thread is starting child thread.");
      System.out.println("main thread is done, now just waiting for child thread to finish.");
     while (childThread.isAlive()) {
         // sleep for some small amount of time to prevent consuming cpu cycles in a tight while loop
          try { Thread.sleep(20); } catch (InterruptedException ex) {}
      System.out.println("child thread has exited. now main thread exiting too");
    } // main

  3. #3
    sathishsengg is offline Member
    Join Date
    Jan 2010
    Rep Power


    I Think, This is you Question , "Parent Thread waits until child finish its work".

    Join() helps you!...
    Just Use

    childThread.join(); // Parent waits until the ChildThread dead
    catch(InterruptedException ie) //should catch Interrupted Exception
    //main Ends...

Similar Threads

  1. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  2. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM
  3. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  4. Parent & Child window issues......
    By jithan in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 09:21 AM
  5. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 09:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts