Daemon thread - strange??

Discussion in 'Java' started by adeon, Apr 23, 2005.

  1. adeon

    adeon Guest

    Hi folks,

    I've encountered an odd daemon thread behavior.
    Have a look at this simple example:

    /** Non-daemon thread */
    class R implements Runnable
    {
    public void run()
    {
    System.out.println("non-daemon is running");
    System.out.flush();
    try
    {
    Thread.sleep(5000); //wait 5s
    }
    catch (Exception e)
    {
    throw new RuntimeException(e);
    }

    System.out.println("non-daemon is exiting");
    System.out.flush();
    }
    }


    public class testClass
    {
    // main method
    public static void main(String[] args)
    {
    //create an anonymous daemon thread
    Thread t = new Thread(){
    public void run()
    {
    System.out.println("daemon is starting");
    //create non-daemon thread and start it
    Thread t2 = new Thread(new R());
    t2.setDaemon(false);
    t2.start();
    System.out.println("daemon is exiting");
    }
    };

    t.setDaemon(true);
    t.start();
    try
    {
    //Thread.sleep(1000); (!!!!!)
    }
    catch (Exception e)
    {}

    System.out.println("main() finished");
    System.out.flush();
    }
    }


    What the code above does is that main() simply creates a daemon thread
    't', whose run() method creates a *non*-daemon thread 't2'. Then
    the daemon thread 't' is started. One could expect that _if_ the daemon
    thread starts the non-daemon thread, the program should run at least for
    5s (because, according to javadoc, the application runs as long as it's
    all non-daemon threads run). But the output I get (when line marked
    (!!!!!) is commented out) :

    main() finished
    daemon is starting
    daemon is exiting
    non-daemon is running

    The "non-daemon is running" message means that the non-daemon thread
    't2' was started, but the application didn't wait for it's finish!!
    Is it a feature, a bug, or am I doing sth wrong?

    On the other hand, when the line marked (!!!!) is not commented out, the
    output is as I expected:

    daemon is starting
    daemon is exiting
    non-daemon is running
    (waits about 1s)
    main() finished
    (waits about 4s)
    non-damon is exiting

    It looks like there was a race condition in the first example, but my
    opinion is that if a *non*-daemon thread is started, the application
    should wait for it, even if that thread execution was started after
    main() end.

    Cheers
    AD
    adeon, Apr 23, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franz Bayer

    CLDC - Daemon Thread

    Franz Bayer, Jul 29, 2004, in forum: Java
    Replies:
    1
    Views:
    371
    Darryl L. Pierce
    Aug 6, 2004
  2. Replies:
    0
    Views:
    435
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    441
    Jean-Paul Calderone
    Mar 20, 2009
  4. Floris Bruynooghe
    Replies:
    1
    Views:
    437
    Floris Bruynooghe
    Mar 24, 2009
  5. Daemon Win32::Daemon;

    , Sep 7, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    233
Loading...

Share This Page