Sie sind auf Seite 1von 4

6/12/13

Aboutt Daemon Threads (SCJP forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
Ghanashyam Apte Greenhorn Joined: May 12, 2001 Posts: 4

Aboutt Daemon Threads


posted 5/22/2001 11:48 PM

Hello Everybody, My understanding of Daemon thread is that daemon threads are stopped when all user threads die.But it doesn't seem to happen in the following program: Daemon Thread t1 dies even before main (user) thread dies.Somebody plz explain. class Th3 extends Thread { int currentValue = 0; Th3(String nm){ super(nm); } public void run(){ while(currentValue<5){ System.out.println(getName()+": "+(currentValue++)); } try{ Thread.sleep(100); }catch(InterruptedException ie){System.out.println(ie);} System.out.println("Exiting run()"); } }

www.coderanch.com/t/199523/java-programmer-SCJP/certification/Aboutt-Daemon-Threads

1/4

6/12/13

Aboutt Daemon Threads (SCJP forum at JavaRanch)

class Client3{ public static void main(String args[]){ Th3 t1 = new Th3("Thread1"); t1.setDaemon(true); t1.start(); System.out.println(t1.isDaemon()); try{ Thread.sleep(500); }catch(InterruptedException ie){System.out.println(ie);} System.out.println(t1.isAlive()); System.out.println("Exiting main()"); } } /* The O/p on my machine is as follows: true Thread1: 0 Thread1: 1 Thread1: 2 Thread1: 3 Thread1: 4 Exiting run() false Exiting main() */ Ghanashyam
Daniel Dunleavy Ranch Hand Joined: Mar 13, 2001 Posts: 276

posted 5/23/2001 2:57 AM

Not positive of this but I think.... Thread dies when its run() is done or if user threads die. In otherwords you can't have a daemon running with no user threads. Dan

Ghanashyam Apte Greenhorn Joined: May 12, 2001 Posts: 4 Daniel Dunleavy Ranch Hand

posted 5/23/2001 11:54 AM

Thanx But my question is how can a daemon thread die before user thread dies? Ghanashyam

posted 5/23/2001 12:10 PM

www.coderanch.com/t/199523/java-programmer-SCJP/certification/Aboutt-Daemon-Threads

2/4

6/12/13

Aboutt Daemon Threads (SCJP forum at JavaRanch)

Joined: Mar 13, 2001 Posts: 276

As I said....when run() is done.

Mikael Jonasson Ranch Hand Joined: May 16, 2001 Posts: 158

posted 5/23/2001 3:28 PM

I'm not sure about this, but aren't deamonthreads supposed to be eternal? Thus, what happends is that when you start the new thread, both threads (the user and the daemon) has the same priority. You then put the userthread to sleep, thus enabling the daemonthread to run. It will then run a few times, sleeping less then the mainthread does, thus never giving control back to the mainthread. Try to increase the limit of your while (say 100 instead of 5) and see if you get the same result... /Mike

sona gold Ranch Hand Joined: Feb 14, 2001 Posts: 234

posted 5/23/2001 5:43 PM

My understanding of Daemon thread is that daemon threads are stopped when all user threads die.But it doesn't seem to happen in the following program: Daemon Thread t1 dies even before main (user) thread dies.Somebody plz explain. daemon threads are stopped when all threads die. but only if the daemon threads are still running when the other threads are dead. here the dameon thread is dying before other threads there is no problem in that but if the other threads had died and the daemon thread would still be running it would be forced to die.
sona<br />SC JP

Dave Vick Ranch Hand Joined: May 10, 2001 Posts: 3244

posted 5/23/2001 6:03 PM

I think there is some confusion here. It sounds like some people are gettng two related facts mixed up. From the Sun tutorial... Stopping a Thread A program doesn't stop a thread like it stops an applet (by calling a method). Rather, a thread arranges for its own death by having a run method that terminates naturally. This would apply to daemon threads too. So, yes, the t1 thread is dying before the other thread because its run method was finished. Also, a program will terminate as soon as there are no non-daemon threads left running (like the grabage collector). to test this try the following: have your daemon thread sleep for a couple of seconds and your main thread not sleep at all - the program should end before the daemon prints all of it

www.coderanch.com/t/199523/java-programmer-SCJP/certification/Aboutt-Daemon-Threads

3/4

6/12/13

Aboutt Daemon Threads (SCJP forum at JavaRanch)

numbers. hth Dave

Dave

Dave Vick Ranch Hand Joined: May 10, 2001 Posts: 3244

posted 5/23/2001 6:16 PM

Sorry, Just ran this myself to make sure I wasn't giving anyone bad scoop and I didn't notice that the daemon thread doesn't sleep until after it is done printing out its numbers. Trying what I said in my first post will still illustrate the daemon dying when the main thread does but it will print out all numbers before it dies it wont print its last message Exiting run(). Sorry if that confused anyone. Dave

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: Aboutt Daemon Threads

Similar Threads daemon threads what is demon thread Thread synchronization problem status of deamon threads Daemon Threads
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 07:20:59 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/199523/java-programmer-SCJP/certification/Aboutt-Daemon-Threads

4/4

Das könnte Ihnen auch gefallen