This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
can u tell me what is a Daemon thread and what actually it does ?
Kalidas Pavi
Ranch Hand
Joined: Nov 20, 2000
Posts: 42
posted
0
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
Joined: Jan 05, 2001
Posts: 12
posted
0
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
Joined: Nov 20, 2000
Posts: 42
posted
0
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
Joined: Nov 20, 2000
Posts: 42
posted
0
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
Joined: Jan 05, 2001
Posts: 532
posted
0
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
Joined: Nov 20, 2000
Posts: 42
posted
0
Jim, This means that main thread cannot be made Daemon??? Kalidas
Jim Baiter
Ranch Hand
Joined: Jan 05, 2001
Posts: 532
posted
0
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.