IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes Deamon Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Deamon Threads" Watch "Deamon Threads" New topic
Author

Deamon Threads

harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 86


What is the result?
A. A done
B. B done
C. A done
B done
D. B done
A done
E. There is no exception that the application will print anything.
F. The application outputs “A done” and “B done”, in no guaranteed order.


Why the answer is E?
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Daemon threads only run as long as there are User Threads (non-daemon threads) running.

Once you start thread A in the main method, the 'main thread' (which is the only remaining user thread) ends. At that point the JVM shuts down the daemon thread. This happens before Thread A has the opportunity to print its own output or create thread B.


Keith Rainey
OCPJP6
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

If you comment out the setDaemon() line, you will probably see it prints out the output lines for both A & B threads. In that situation, you are not creating a Daemon thread. Both A & B are user threads.
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
call the sleep() method just after you call the start() method of Thread A, this will validate Keith's reply
 
 
subject: Deamon Threads
 
Threads others viewed
Thread Question.
setDaemon method
Tell me the output and how u go it please....
Thread question repuired
Doubt on setDeamon().
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture