File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

daemon thread misbehaving

 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a better example below at 4:13 AM. (Which is 3:13AM where I live!)
Would you please explain why the daemon thread does not terminate after the main method finishes?

[ October 14, 2003: Message edited by: Marlene Miller ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that if you increase the sleep time enough, you can see that the child thread will not finish. So perphaps in your example, by the time the child thread receives the signal from the main thread to end, it had already ended.
[ October 13, 2003: Message edited by: Alton Hernandez ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Alton for your observation.
If I change the run method to this, the program prints run for a while and then stops.

run1 ... run188 run189 run190 run191 run192
run1 ... run50 run51 run52
run1 ... run101 run102 run103
[ October 14, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The daemon thread is executing although the main thread is not alive. Why is this?

main
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsePress
any key to continue . . .
(Fixed the bug Alton found.)
[ October 14, 2003: Message edited by: Marlene Miller ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene, your code doesn't compile!
I believe you meant "Thread t = new Thread(new R(Thread.currentThread()))" ?
[ October 14, 2003: Message edited by: Alton Hernandez ]
 
Kalai Ganesh
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
Here is a small explanation about daemon threads.
The program can run to completion even if some or all of the daemon threads have not run (say, it is still running, waiting or sleeping).
Hope this helps.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops. Bug. Thank you Alton. Yes I meant new R().
After I fixed it, I sometimes get the same results, and other times I get
main
truetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetrue
truetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetrue
truetruetruetruetruetruetruetruetruetruetruefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsef
alsefalsefalsefalsefalsePress any key to continue . . .
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalai, thank you for your comments.
I did not state explicitly why I think the daemon thread is misbehaving. I assumed a program exits when all of the user threads have terminated, even if there are runnable daemon threads. My example contradicts the assumption. The program does not exit when all the user threads have terminated. It runs a little while longer.
The Java Programming Language says When the last user thread is finished, any daemon threads are terminated and the application is finished. This termination is like that of invoking destroy�abrupt and with no chance of cleanup.
I think I misunderstood �abrupt�. Abrupt describes how, not when.
Concurrent Programming in Java, Doug Lea says The method setDaemon asserts that the JVM may exit, abruptly terminating the thread, so long as all other non-daemon threads in the program have terminated.
[ October 14, 2003: Message edited by: Marlene Miller ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you'd be safe in assuming that the JVM will exit after all non-daemon threads have completed - but there's no guarantee just how quickly this will occur. I strongly doubt that the real exam will depend on this distinction (mine did not). No idea about other mock exams, but I say that for purposes of certification, don't worry about it.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jim. I wasn't reading carefully.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic