• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deamon Thread Sample

 
Richard Teston
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Examine code below:

When I set t1.setDaemon(true) the following output is produce....

....and when I set t1.setDaemon(true) and t2.setDaemon(true)the following output is produce:

I'm expecting that when you set t1 as a daemon thread it should be stop executing when the main thread dies. Why does daemon thread t1 still execute when its parent thread main thread is already
in a dead state?
 
Richard Teston
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it!, daemon thread still executes when there is still at least one user thread executing in my code case when i set t1.setDaemon(true) I have t1 as a daemon thread and my t2 and main thread are my user thread. When main thread stop executing, t1(daemon thread) will continue to execute because t2(user thread) is still alive. When i set t1 and t2 as a daemon thread their lives is in the hand of main(user thread), so when main thread stop executing t1 and t2 (daemon thread) will also stop executing. em I making any sense?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're making sense. There's nothing special about the main thread. In many graphical programs, the main thread exits as soon as it set up the GUI; the GUI event thread, a non-daemon thread, keeps the application running.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!