当前位置: 技术问答>java相关
能解释一下daemon线程是什么玩意?
来源: 互联网 发布时间:2017-04-11
本文导语: 在什么时候用,能否例一个小例子. | Daemon threads A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essenc...
在什么时候用,能否例一个小例子.
|
Daemon threads
A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated. Conversely, if there are any non-daemon threads still running, the program doesn’t terminate. There is, for instance, a thread that runs main( ). Comment
You can find out if a thread is a daemon by calling isDaemon( ), and you can turn the “daemonhood” of a thread on and off with setDaemon( ). If a thread is a daemon, then any threads it creates will automatically be daemons. Comment
The following example demonstrates daemon threads:
//: c14:Daemons.java
// Daemonic behavior.
// {RunByHand}
import java.io.*;
class Daemon extends Thread {
private static final int SIZE = 10;
private Thread[] t = new Thread[SIZE];
public Daemon() {
setDaemon(true);
start();
}
public void run() {
for(int i = 0; i
A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated. Conversely, if there are any non-daemon threads still running, the program doesn’t terminate. There is, for instance, a thread that runs main( ). Comment
You can find out if a thread is a daemon by calling isDaemon( ), and you can turn the “daemonhood” of a thread on and off with setDaemon( ). If a thread is a daemon, then any threads it creates will automatically be daemons. Comment
The following example demonstrates daemon threads:
//: c14:Daemons.java
// Daemonic behavior.
// {RunByHand}
import java.io.*;
class Daemon extends Thread {
private static final int SIZE = 10;
private Thread[] t = new Thread[SIZE];
public Daemon() {
setDaemon(true);
start();
}
public void run() {
for(int i = 0; i