aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes daemon threads 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 "daemon threads" Watch "daemon threads" New topic
Author

daemon threads

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Are daemon threads included for the SCJP1.4 exam?

If yes, what is needed to know about daemon threads?

Thanks in advance,
rajani.
erto zhang
Greenhorn

Joined: May 18, 2004
Posts: 7
I am not sure, but I met them in the mock exam so I prepare it. I think what you only need to know is:
1. Daemon thread is low priority thread which will provide the resource for your (user) thread.
2. If all user threads are dead the Daemon threads are dead automatically and your application is over as well.
3. You must set thread as daemon before you start it.
4. The thread started from daemon thread is a daemon thread too.
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Thankyou..

regards,
rajani.
Raj Poosarla
Greenhorn

Joined: May 22, 2004
Posts: 8
Another point to note is that, a program using daemon thread will run to completion even if the daemon thread is still running, waiting or sleeping.

Here is an example to understand how it works:

class A extends Thread {
public void run() {
synchronized (this) {
try {wait();} catch (InterruptedException ie){}
}}
public static void main(String[] args) {
A a1 = new A();
a1.setDaemon(true);
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
}}

Which is a possible result of attempting to compile and run the program?

a. The number printed is greater than or equal to 0
b. The synchronized block inside the run method is not necessary
c. Thread a1 waits forever and the program runs forever
d. Compile-time error
e. Run-time error
f. None of the above


Sol: The number printed is greater than or equal to 0 as the program will complete.

Regards,
-Raj Poosarla

---------------------------

Success is sweet but secret is sweat
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Hi Raj..

Good example..

thankyou...

regards,
rajani.
Wendal Park
Ranch Hand

Joined: Aug 28, 2001
Posts: 39
Daemon threads are also called "Service threads" ... an example would be the garbage collection thread inside a JVM.

Just a note to point out that you have to set a thread to be a daemon thread prior to calling yourThread.start(), otherwise calling [yourThread.setDaemon(true)[/i] thereafter will not change the thread's status. This should be easy to understand though ...


MSc, BSc, SCJP 1.4<br /> <img src="graemlins/banghead.gif" border="0" alt="[banghead]" /> SCBCD
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: daemon threads