I want to search the Python multi-threaded Setdaemon, found this article is very good: http://blog.csdn.net/m13666368773/article/details/7245570
The use of Thread.setdaemon, after learning to understand:
1. Setdaemon needs to be used before the Start method call
2. Thread is divided into user thread and background (daemon) process, Setdaemon set thread as background process
3. If the JVM is a background process, the current JVM will exit. (with it, everything disappears, including the background thread)
4. After the main thread ends,
1) The user thread will continue to run
2) If there is no user thread, it is a background process, then the JVM ends
Other than that:
The Setdaemon method sets the Java thread to the daemon thread, and the invocation of this method must be performed before the threads are started. The JVM exits only if all threads in the current JVM are daemon threads.
If you create a thread that does not display this method, this defaults to the user thread.
Examples are as follows:
Package com.jack.mySample;
Import java.io.IOException;
public class Testthread extends Thread {
Public Testthread () {
}
/** *//**
* Thread's Run method, which runs concurrently with other threads
*/
public void Run () {
for (int i = 1; i <=; i++) {
try{
Thread.Sleep (100);
} catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
System.out.println (i);
}
}
public static void Main (String [] args) {
Testthread test = new Testthread ();
If daemon is not set, then the thread will output 100 before it ends
Test.setdaemon (TRUE);
Test.start ();
System.out.println ("Isdaemon =" + Test.isdaemon ());
try {
System.in.read (); Accepts input, causes the program to pause at this point, once the user input is received, the main thread ends and the daemon thread ends automatically
} catch (IOException ex) {
Ex.printstacktrace ();
}
}
}
Additional notes:
Definition: A daemon thread-also known as a "service thread"-automatically leaves when no user thread is available to serve.
Priority: The daemon thread has a lower priority, which is used to service other objects and threads in the system.
Settings: Set the thread to "daemon" by Setdaemon (true); Set a user thread to
The way to daemon threads is to use the Setdaemon method of the thread object before the thread object is created.
Example: The garbage collection thread is a classic daemon thread, and when our program no longer has any running
Thread, the program will no longer produce garbage, the garbage collector will have nothing to do, so when the garbage collection thread is
The garbage collection thread will automatically leave the remaining threads on the JVM. It is always running in a low-level state for
Real-time monitoring and management of recyclable resources in the system.
Life cycle: The Daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and
Perform certain tasks periodically or wait to handle certain occurrences. That is
The daemon thread does not depend on the terminal, but relies on the system, "die" with the system. The Java daemon thread is
What it looks like. The JVM can exit when all the threads in the JVM are daemon threads;
or more non-daemons, the JVM does not exit.
The first encounter this method, was this eccentric spelling words under, check API and feel not deep understanding, that still check daemon the word has what meaning, what etymology it. AE in Daemon is a tone, accented in/di:/ , it seems to mean the Greek guardian God. In Computer professional English is the meaning of the Guardian thread. Thought that if a thread is honored as a daemon thread, it will not be until the process runs to the last moment, but really wrong, if a thread is a daemon, then, if no non-daemon threads run at the end of the main thread, the daemon will commit suicide. The Setdaemon method is the method in thread, which defaults to false, marking the thread as a daemon or user thread, which must be called before the thread is started, with the lowest priority, allowing system resources to invoke other threads first. When a running thread is a daemon thread, the Java virtual machine exits.
As the following small program is shown below:
Import java.io.IOException;
Class TestMain4 extends Thread {
public void Run () {//perpetual loop thread
for (int i=0;; i++) {
try {
Thread.Sleep (1000);
} catch (Interruptedexception ex) {}
System.out.println (i);
}
}
public static void Main (String [] args) {
TESTMAIN4 test = new TestMain4 ();
Test.setdaemon (TRUE); When debugging can be set to False, then this program is a dead loop, there is no exit condition. Set to True, the main thread ends, and the test thread ends.
Test.start ();
System.out.println ("Isdaemon =" + Test.isdaemon ());
try {
System.in.read (); Accepts input, causes the program to pause at this point, once the user input is received, the main thread ends and the daemon thread ends automatically
} catch (IOException ex) {}
}
}
Go Thread.setdaemon Setup Instructions