Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

daemon threads

 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou..

regards,
rajani.
 
Raj Poosarla
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj..

Good example..

thankyou...

regards,
rajani.
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic