• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deamon Threads

 
harshada patil
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
call the sleep() method just after you call the start() method of Thread A, this will validate Keith's reply
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!