Java守护线程

来源:互联网 发布:ubuntu没有软件中心 编辑:程序博客网 时间:2024/06/13 01:15

当JVM中所有的线程都是守护线程的时候,JVM就可以退出了;

如果还有一个或以上的非守护线程则不会退出。

和普通线程写法的区别就是:

setDaemon(true);

demo

public class DeamonThread extends Thread {public void run() {try {for (int i = 0; i < 10; i++) {System.out.println("线程执行了"+ i);Thread.sleep(1000);}} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {Thread thread = new DeamonThread();thread.setDaemon(true);thread.start();try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


 

原创粉丝点击