什么是Java中的守护进程线程?有人能告诉我Java中有什么守护进程线程吗?
3 回答
qq_遁去的一_1
TA贡献1725条经验 获得超8个赞
setDaemon(boolean)Thread
哔哔one
TA贡献1854条经验 获得超8个赞
创建新线程时,它继承其父线程的守护进程状态。 当所有非守护进程线程完成时,jvm将停止,并且任何剩余的线程都会停止。 守护进程线程被放弃:由于这个原因,应该谨慎地使用守护进程线程,使用它们执行任何类型的I/O任务都是危险的。 最后,没有执行块。,
堆栈没有打开-JVM只是退出。 由于这个原因,应该谨慎地使用守护进程线程,使用它们执行任何类型的I/O任务都是危险的。
慕的地10843
TA贡献1785条经验 获得超8个赞
setDaemon.
public class DaemonTest {
public static void main(String[] args) {
new WorkerThread().start();
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}
System.out.println("Main Thread ending") ;
}
}
class WorkerThread extends Thread {
public WorkerThread() {
// When false, (i.e. when it's a user thread),
// the Worker thread continues to run.
// When true, (i.e. when it's a daemon thread),
// the Worker thread terminates when the main
// thread terminates.
setDaemon(true);
}
public void run() {
int count = 0;
while (true) {
System.out.println("Hello from Worker "+count++);
try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
}
}添加回答
举报
0/150
提交
取消
