Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is a Daemon Thread  RSS feed

 
ganshre
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u tell me what is a Daemon thread and what actually it does ?
 
Kalidas Pavi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ganshre,
JVM treats threads as either non Daemon user threads or Daemon threads. A thread can be set to Daemon status by
1. Using the method setDaemon(true) before the thread is started via start() method call.
2. When a Daemon thread creates a new thread the new thread automatically inherits the Daemon status of its parent thread. In this case a setDaemon(true) call is not required for making the thread Daemon.In other words, all threads created by Daemon threads will be Daemonss!!!
When an application is started, the main method thread is executed as a user thread and if there are no other user threads created by the main method the application will exit once the main method thread is completed.
If JVM finds out that only Daemon threads are running in the application, (ie all the user threads are stopped) JVM will close down the application and exit.
The main advantage of Daemon threads are that they are automatically killed by the JVM once no other threads are existing. We don't need to worry about stopping the Daemon threads. But be careful not to assign any important tasks in Daemon threads as Daemons may end abruptly without your consent once all other threads are stopped and thereby creating havoc.
Hope I have made it somewhat clear...
Kalidas.
 
ganshre
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u very much Kalidas. u r saying that Main Thread is not a Daemon thread. how to set a main thread to daemon thread. because whenever u run a pgm the main thread 'll be invoked. but before starting the main thd. how to set it as a daemon thread. can u give me an example for unimportant task.
 
Kalidas Pavi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to define a main method as Daemon??
Further main is given a special treament by JVM as it is the method JVM looks for in an application to execute after the class initializations are done.
I think we are not allowed to change the properties of the main method.
Kalidas
 
Kalidas Pavi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to make the main thread Daemon with the following code
public class MyThread {
public static void main(String[] args)
{
Thread t = Thread.currentThread(); // returns the main thread
t.setDaemon(true); //will throw illegal thread state exception
}}
This program compiles without any glitch, but it throws a run time exception IllegaThreadStateException. setDaemon(true) is not allowed for main thread.
Need some more light from the experts out there!!
Kalidas.
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't make a thread daemon if it is already started.
Since you are trying to do this to currentThread, this is
your problem.
 
Kalidas Pavi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
This means that main thread cannot be made Daemon???
Kalidas
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you look at the VM spec, section 2.19, the VM will
start a non-daemon thread that calls your main method. The
API spec for Thread.setDaemon says "This method must be called
before the thread is started". At any rate, when you are calling
Thread.currentThread(), you are definitely referencing a thread
that has been started thus setDaemon will fail.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!