You are on page 1of 4

7/14/13

need clarification on threads (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

need clarification on threads


posted 11/4/2003 5:14 AM

dhana rangu Ranch Hand Joined: Oct 01, 2003 Posts: 65

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

Eddie Long Ranch Hand Joined: Nov 02, 2003 Posts: 69

posted 11/4/2003 5:59 AM

"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
www.coderanch.com/t/243670/java-programmer-SCJP/certification/clarification-threads 1/4

7/14/13

need clarification on threads (SCJP forum at JavaRanch)

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 ]

...and so help me God.

dhana rangu Ranch Hand Joined: Oct 01, 2003 Posts: 65

posted 11/4/2003 6:24 AM

thanx dayo for ur reply

Kathy Sierra C owgirl and Author Ranch Hand Joined: Oct 10, 2002 Posts: 1572

posted 11/4/2003 6:47 AM

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

www.coderanch.com/t/243670/java-programmer-SCJP/certification/clarification-threads

2/4

7/14/13

need clarification on threads (SCJP forum at JavaRanch)

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 Joined: Nov 02, 2003 Posts: 69

posted 11/4/2003 3:06 PM

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 Joined: Oct 01, 2003 Posts: 65

posted 11/5/2003 3:26 AM

thanks a lot kathy.I got what u said... my thanks to dayo also

Granny's Programming Pearls "inside of every large program is a small program struggling to get out"
www.coderanch.com/t/243670/java-programmer-SCJP/certification/clarification-threads 3/4

7/14/13

need clarification on threads (SCJP forum at JavaRanch)

JavaRanch.com/granny.jsp

subject: need clarification on threads

Similar Threads what are daemon threads? Thread Question from Dan's mock exam Running java applications in virtual machine without a terminal User threads versus Daemon Threads Question from exam on Daemon thread
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:36:50 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/243670/java-programmer-SCJP/certification/clarification-threads

4/4

You might also like