Output: -------- null null Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005 Tue Aug 30 06:14:49 EDT 2005
Why the first two value is NULL. What is the use Daemon thread? What is the role of setDaemon(true)? What will happen if setDaemon(false)? I had tries setDaemon(false), but no differences in the output. [ August 30, 2005: Message edited by: Mark Henryson ]
Unless you have a multi-processor machine, the two threads have to take it in turns to run. The first two values are null because the main thread has the processor and ds will not have been set until the child thread gets the processor. Even if you have a multiprocessor machine, you may still get some nulls, because there is some initialisation of the child thread to be done before the run method is called.
Check out the API docs for what a daemon thread is.
A Daemon is a "background" thread that will not prevent a program from ending.
In the following example, two threads are created. One has an infinite loop and the other has a finite loop. But the thread with the infinite loop is a Daemon, so it does not prevent the program from ending after the non-Daemon thread completes.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
You already have 2 threads about threads moved to the threads forum, this very day. Since you don't seem to understand why they were moved let me explain: The threads forum is for threads about discussing threads and any threads related topics deserve their own thread in there.
That way, you save the nice people around here the work of manually moving them and you have a much bigger chance of the right people reading your question so they can help you. Everyone wins that way!