java中有两类线程,user thread(用户线程)和daemon thread(守护线程)。这两种线程本质一样,只不过如果用户线程全部退出,那么守护线程也就没什么好执行的了,java虚拟机就会退出。也就是守护线程是用户线程提供服务的,如果用户线程退出,那么守护线程也就没什么好维护的了。
设置守护线程:
public final void setDaemon(boolean on);
但是注意一点:
thread.setDaemon(true)必须在thread.start()之前设置,你不能把正在运行的常规线程设置为守护线程,否则会抛出一个IllegalThreadStateException的异常。
看代码:
public class DaemonTest implements Runnable{
@Override
public void run() {
try{
Thread.sleep(1000);
System.out.println("开始写入daemon");
File file = new File("d://daemon.txt");
FileOutputStream fos = new FileOutputStream(file, true);
fos.write("daemonThread".getBytes());
fos.flush();
fos.close();
}catch (IOException e){}
catch (InterruptedException e){}
}
public static void main(String[] args) {
Thread t = new Thread(new DaemonTest());
t.setDaemon(true);
t.start();
}
}
可以发现,daemon.txt并没有被写入。因为你设置这个线程为守护线程,而此时并没有需要维护的用户线程,所以java虚拟机也就推出了,写入工作也没有执行。
换个方式:
public class DaemonTest implements Runnable{
@Override
public void run() {
try{
Thread.sleep(1000);
System.out.println("开始写入daemon");
File file = new File("d://daemon.txt");
FileOutputStream fos = new FileOutputStream(file, true);
fos.write("daemonThread".getBytes());
fos.flush();
fos.close();
}catch (IOException e){}
catch (InterruptedException e){}
}
public static void main(String[] args) {
Thread t = new Thread(new DaemonTest());
new Thread(){
@Override
public void run() {
try{
Thread.sleep(2000);
System.out.println("开始写入daemon2");
File file = new File("d://daemon2.txt");
FileOutputStream fos = new FileOutputStream(file, true);
fos.write("daemonThread".getBytes());
fos.flush();
fos.close();
}catch (IOException e){}
catch (InterruptedException e){}
}
}.start();
t.setDaemon(true);
t.start();
}
}
此时因为有用户线程正在运行,所以守护线程也就可以正常执行,daemon.txt和daemon2.txt都可以被写入。