停止进程
public class MyThread extends Thread {
private boolean threadDone = false;//设置标志位来停止进程
public void done() {
threadDone = true;
}
public void run() {
while (!threadDone) {
// work here
// modify common data
}
}
}
Daemon 守护进程
当程序在没有任何非守护进程的情况下,则jvm可以随时退出,不用在意守护进程是否存在。
例如写word文档中,拼写检查就是守护进程(在拼写错的时候,它会提示错误,但是你可以进行进行写文档)。
设置某个进程为守护进程----thread.setDaemon(true);
一个普通的java程序:系统会有自己的守护进程―――
Thread:Reference Handler
Priority:10
Daemon
Thread:Finalizer
Priority:8
Daemon
Thread:Signal Dispatcher
Priority:9
Daemon
Thread:Attach Listener
Priority:5
Daemon