Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Question from exam on Daemon thread

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question i remember from the test had a line:
Daemon Thread(true)...or something like that...
how does this affect the thread code around it?
[ November 12, 2003: Message edited by: Gary Peck ]
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gary, I can't remember the link to the relevant thread, but I saved it locally when I was preparing for the exam. Hopefully, it'll help you too:
Kathy's clarification on Daemon threads


Kathy Sierra
sheriff
Member # 37766
posted November 03, 2003 06:17 PM
---------------------------------------------------------------------------
Howdy -- A daemon thread does *not* have to do with thread priority... it's about whether the thread is a *support* thread vs. a regular (often called "user") thread. When I say "support" I mean that a daemon thread is a thread that you don't want to live on its own. When you have a daemon thread, it means you don't want that thread to continue if all of the other non-daemon threads are done. So, when the last non-daemon ("user") thread completes, any daemon threads will automatically terminate.
To make a thread a daemon thread, you simply call theThread.setDaemon(true), and you can test it with aThread.isDaemon().
A thread will inherit its daemon status from the thread that created it... so if a deamon thread creates a thread, the new thread will be a daemon thread.
Now... for the exam, you need to know only THIS about daemon threads:
* A daemon thread is created by calling setDaemon(true) on a thread object.
* A daemon thread will terminate when there are no more user (non-daemon) threads running.
So... think about the implications if a user thread starts a daemon thread, and then the user thread immediately terminates. Will the daemon thread show its output? (assuming the daemon thread had something in its run method). You don't know! It all depends on whether the scheduler decided to make the daemon thread the running thread *before* allowing the user thread to complete its own run method. If the user thread completes, the daemon thread will never have a chance to run. But if the scheduler decided to pull the daemon thread into the "running" status as soon as the user thread calls start() on it, then the daemon thread *could* show some output from its run method.
For example:
// in a Runnable somewhere:
public void run() {
Thread a = new MyDaemonThread();
// a thread that makes itself a daemon in its own constructor
a.start();
} // now run completes. But what happened first? Did a.start() cause the MyDaemonThread's own run method to run?
OK, that's the sum total of what you need for the exam. We did NOT mention this in the cert book! But we can assure you that since daemon threads are not mentioned explicitly in the exam, the most you will have on the exam is ONE question that requires actual daemon knowledge to answer.
But remember, you may have questions that *use* the concept of daemon threads, but do not require any knowledge of daemon threads to answer the question. For all you know, a question with a daemon thread in it might be a question on inner class syntax.
Everything you need to know about daemon threads is in this post!!!
(you'll have to trust me on this one
cheers,
Kathy
// consider this post "Something that should have been in the book but wasn't."
Thanks for this question!!

reply
    Bookmark Topic Watch Topic
  • New Topic