Forums Register Login

what is a Daemon Thread

+Pie Number of slices to send: Send
can u tell me what is a Daemon thread and what actually it does ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Jim,
This means that main thread cannot be made Daemon???
Kalidas
+Pie Number of slices to send: Send
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.
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1996 times.
Similar Threads
Daemon Thread
Daemon threads & non daemon Threads
what is daemon thread
Daemon Thread
Daemon threads
regarding daemon thread in java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:19:10.