• 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

need clarification on threads

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
i am absolutely new to java .I am preparing for scjp.can any one help me in this.....
"when the last user thread completes ,any daemon threads are stopped and the application stops
a thread's default daemon status is the same as that of thread creating it"
from the words quoted ,I could not understand what a daemon is and what daemon status means...Could any one give an idea of what it means...
thanx in anticipation
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"when the last user thread completes ,any daemon threads are stopped and the application stops


I think the daemon threads are referring to background threads i.e. threads spawned by the main application thread


a thread's default daemon status is the same as that of thread creating it"


I think the daemon status refers to the priority of the thread. All threads take on the priority of the thread that spawned them if their priority is not set before the start method is called e.g.


...
Runnable r = new SomeRunnableImplementer()
Thread th = new Thread(r);
th.setPriority(5);
th.start();
....


Good luck
Dayo
[ November 03, 2003: Message edited by: Syberd ]
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx dayo for ur reply
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Eddie Long
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well! what can I say but thanks Kathy for that post and Sorry for almost misleading you dhana.
I assumed your book was just using a different name for background thread i.e. daemon thread
Dayo
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot kathy.I got what u said...
my thanks to dayo also
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic