守护线程

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都可以被写入。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值