wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tell me the output and how u go it please.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tell me the output and how u go it please...." Watch "Tell me the output and how u go it please...." New topic
Author

Tell me the output and how u go it please....

Manoj Mani
Ranch Hand

Joined: Mar 31, 2007
Posts: 65
public class A extends Thread
{
A()
{
setDaemon(true);
}
public void run()
{
(new B()).start();
try
{
Thread.sleep(60000);
}
catch (InterruptedException x)
{
}
System.out.println(�A done�);
}

class B extends Thread
{
public void run()
{
try
{
Thread.sleep(60000);
}
catch (InterruptedException x)
{
}
System.out.println(�B done�);
}
}

public static void main(String[] args)
{
(new A()).start();
}
} //Please tell me the use of setDaemon(true);


Impossible Is Often Untried.Its Time For Us To Change....
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Just a suggestion...for finding out what setDeamon does google is best...I got this when I queried for it

setDaemon
public final void setDaemon(boolean on)Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
This method must be called before the thread is started.

--------------------------------------------------------------------
The the second thing I would suggest is running the code in NetBeans...first commenting out this setDaemon and then uncommenting out the setDaemon...and then looking into the defination I found above.

Wow...that tells me that the output would be nothing in case of this question, bcoz the JVM exited as only the daemon thread was running. If the setDaemon would not have been called, then I would have got

"A done" and "B done" printed in undetermined order.

Good luck
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Good response megha joshi. It's a good idea to use the java API to find out about such things (keep an http link to it or copy the Java docs directly from Sun's website).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
GeeCON Prague 2014
 
subject: Tell me the output and how u go it please....