Java多线程(三)守护线程和非守护线程

基本概念

  • 守护线程:和主线程一起结束的线程,叫守护线程。
  • 非守护线程:主线程的结束不影响线程的执行的线程,也叫用户线程。

设置守护线程

/* Whether or not the thread is a daemon thread. */
    private boolean     daemon = false;

daemon变量用户判断是否为守护线程

 /**
     * Marks this thread as either a {@linkplain #isDaemon daemon} thread
     * or a user thread. The Java Virtual Machine exits when the only
     * threads running are all daemon threads.
     *
     * <p> This method must be invoked before the thread is started.
     *
     * @param  on
     *         if {@code true}, marks this thread as a daemon thread
     *
     * @throws  IllegalThreadStateException
     *          if this thread is {@linkplain #isAlive alive}
     *
     * @throws  SecurityException
     *          if {@link #checkAccess} determines that the current
     *          thread cannot modify this thread
     */
    public final void setDaemon(boolean on) {
        checkAccess();
        if (isAlive()) {
            throw new IllegalThreadStateException();
        }
        daemon = on;
    }

调用setDaemon(boolean on) 即可设置

示例代码

非守护线程
public class MyThread  extends Thread{


    @Override
    public void run() {
        //重写run方法

        for(int i=1;i<11;i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程:"+Thread.currentThread().getName()+"执行了"+i+"次");
        }

    }

    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        myThread.start();
        for(int i=1;i<11;i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程:"+Thread.currentThread().getName()+"执行了"+i+"次");
        }
    }
}

打印结果:由结果可以看出主线程的正常结束并不会影响子线程的执行,子线程继续执行。

线程:main执行了1次
线程:Thread-0执行了1次
线程:Thread-0执行了2次
线程:main执行了2次
线程:main执行了3次
线程:main执行了4次
线程:main执行了5次
线程:main执行了6次
线程:Thread-0执行了3次
线程:main执行了7次
线程:Thread-0执行了4次
线程:main执行了8次
线程:Thread-0执行了5次
线程:main执行了9次
线程:Thread-0执行了6次
线程:main执行了10次
线程:Thread-0执行了7次
线程:Thread-0执行了8次
线程:Thread-0执行了9次
线程:Thread-0执行了10次

守护线程
public class MyThread  extends Thread{


    @Override
    public void run() {
        //重写run方法

        for(int i=1;i<11;i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程:"+Thread.currentThread().getName()+"执行了"+i+"次");
        }

    }

    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        myThread.setDaemon(true);
        myThread.start();
        for(int i=1;i<6;i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程:"+Thread.currentThread().getName()+"执行了"+i+"次");
        }
    }
}

打印结果:由结果可以看出主线程结束后,设置为守护线程的子线程将不再执行。

线程:Thread-0执行了1次
线程:main执行了1次
线程:main执行了2次
线程:Thread-0执行了2次
线程:Thread-0执行了3次
线程:main执行了3次
线程:main执行了4次
线程:Thread-0执行了4次
线程:Thread-0执行了5次
线程:main执行了5次

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 之前介绍过了美军部队训练用的《神奇的两分钟入睡法》,这次再介绍多一个睡前的冥想训练,可以相互结合着使用,效果更好哦...
    南方的南007阅读 12,786评论 0 2
  • 太逞强真的好嘛? 有什么办不到的事情为何不去拒绝? 不懂拒绝?不会拒绝? 蔡先生时常提醒我要自私些, 不要只顾头不...
    麦太和肉安阅读 60评论 0 1
  • 2018年7月28日周六 1、5点起床读书《男人来自火星,女人来自金星》 2、今天中午约了一位人生中的贵人去参加索...
    小草草小阅读 207评论 0 0
  • JDK 1.6: /System/Library/Java/JavaVirtualMachines/1.6.0.j...
    devin_yuer阅读 804评论 0 0