• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

daemon threads

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou..

regards,
rajani.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj..

Good example..

thankyou...

regards,
rajani.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic