aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes daemon thread misbehaving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "daemon thread misbehaving" Watch "daemon thread misbehaving" New topic
Author

daemon thread misbehaving

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: May 30, 2003
Posts: 443
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: May 30, 2003
Posts: 443
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

Joined: Sep 08, 2003
Posts: 32
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Jim. I wasn't reading carefully.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: daemon thread misbehaving