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.
     *
     * 

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次

你可能感兴趣的:(Java多线程(三)守护线程和非守护线程)