What will happen to a thread treated as Daemon?
What will be the effect of this to the thread?
W开发者_如何学Gohat are the "can and can'ts" on the thread?
A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. Daemon threads are service providers for other threads running in the same process as the daemon thread
E.g Garbage Collection.
You can explicitly specify a thread created by a user thread to be a daemon thread by calling setDaemon(true)
on a Thread object.
For your reference,
Note that the
setDaemon()
method must be called before the thread'sstart()
method is invoked.Once a thread has started executing (i.e., itsstart()
method has been called) its daemon status cannot be changed. To determine if a thread is a daemon thread, use the accessor methodisDaemon()
.
The main difference between a daemon thread and a non-daemon thread is that a program terminates when all non-daemon threads have terminated. So if you've got an active daemon thread and end your first thread, the program terminates. So you'd want to use a daemon thread for something you want to keep doing as long as the program is running.
What will happen to a thread treated as Daemon?
The flag isDaemon()
will be set to true.
What won't happen is; this thread won't stop the application from exiting.
What will be the effect of this to the thread?
Nothing unless you look at the isDaemon() method.
What are the "can and can'ts" on the thread?
You can do anything in a daemon thread you can do in a non-daemon thread.
You can't guarantee the thread will finish.
Difference between Daemon and Non Daemon thread in Java :
1) JVM doesn't wait for any daemon thread to finish before existing.
2) Daemon Thread are treated differently than User Thread when JVM terminates, finally blocks are not called, Stacks are not unwounded and JVM just exits.
Daemon thread is one which won't prevent the program from ending.
take an example :
Java application starts off with just one thread, the "main" thread which runs the main
method. This is a regular (non-daemon) thread. Now imagine again that the program starts another thread which seats in a loop doing something. If the new thread is a regular (non-daemon) thread, it will prevent the program from finishing when "main" ends, and keep going forever!
class HelloThread extends Thread
{
public void run()
{
for ( ; ; )
{
System.out.println("hello");
sleep(1000);
}
}
}
public class RegularThreader
{
public static void main(String[] args)
{
Thread hello = new HelloThread();
hello.start();
System.out.println("Sorry, I must be leaving");
}
}
Now, this isn't always what is required. Sometimes you want to end this "background" processing when the program finishes. To do this, you can mark threads as daemon threads. Daemon threads don't prevent the program from ending, but stop when the main thread, stops
public class DaemonThreader
{
public static void main(String[] args)
{
Thread hello = new HelloThread();
hello.setDaemon(true);
hello.start();
System.out.println("Sorry, I must be leaving");
}
}
Try running the two different classes above, and see how the output is different. A classic example of a daemon thread is the garbage collector thread found in many Java Virtual Machines. It needs to run continuously while any other threads are running, but should not prevent the program from exiting. When the program exits, there is no more need for a garbage collector.
精彩评论