1. 用户线程:也叫工作线程,当线程的任务执行完或通知方式结束
    2. 守护线程:一般是为工作线程服务的,当所有的用户线程结束,守护线程自动结束
    3. 常见的守护线程:垃圾回收机制 ```java package test;

    public class Main { public static void main(String[] args) throws InterruptedException { MyDaemonThread myDaemonThread = new MyDaemonThread(); //如果我们希望当main线程结束后,子线程自动结束 //,只需将子线程设为守护线程即可 myDaemonThread.setDaemon(true);

    1. myDaemonThread.start();
    2. for( int i = 1; i <= 5; i++) {//main线程
    3. System.out.println("主线程进行中~");
    4. Thread.sleep(1000);
    5. }
    6. }

    }

    class MyDaemonThread extends Thread { @Override public void run() { for (; ; ) {//无限循环 try { Thread.sleep(1000);//休眠1000毫秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(“子线程进行中~~~”); } } } ``` image.png