📜  Java守护线程和用户线程的区别

📅  最后修改于: 2021-09-10 03:08:54             🧑  作者: Mango

在Java,有两种类型的线程:

  1. 守护线程
  2. 用户线程

守护线程是总是在后台运行的低优先级线程,而用户线程是总是在前台运行的高优先级线程。用户线程或非守护进程旨在执行特定或复杂的任务,其中守护进程线程用于执行支持任务。

Java守护线程和用户线程的区别

  1. JVM 不等待守护线程完成,而是等待用户线程:守护线程和用户线程之间的首要区别在于,JVM 不会等待守护线程完成其任务,而是等待任何活动的用户线程。
    例如,在 NetBeans 中运行Java程序时可能已经注意到这种行为,即使主线程已完成,左上角的向下按钮仍然是红色的,表明Java程序仍在运行。这是由于从主线程产生的任何用户线程,但在主线程中,在 NetBeans 中看不到那个红点。

  2. 线程优先级:与守护线程相比,用户线程具有高优先级,这意味着它们不会像用户线程那样容易获得 CPU。
  3. 创建线程:用户线程通常由应用程序创建,用于并发执行某些任务。另一方面,守护线程主要由 JVM 创建,就像一些垃圾收集作业一样。
  4. 线程终止:JVM将迫使守护线程终止,如果所有用户线程都执行完毕,但用户线程是通过应用程序或通过自我封闭。用户线程可以由 JVM 运行保持运行,但守护线程不能由 JVM 保持运行。这是用户线程和守护线程之间最关键的区别
  5. 用法:守护线程不用于任何关键任务。任何重要的任务都由用户线程完成。守护线程一般用于一些非关键任务的后台任务。

用户线程和守护线程的主要区别:

User Thread Daemon Thread
JVM wait until user threads to finish their work. It never exit until all user threads finish their work. The JVM will’t wait for daemon threads to finish their work. The JVM will exit as soon as all user threads finish their work.
JVM will not force to user threads for terminating, so JVM will wait for user threads to terminate themselves. If all user threads have finished their work JVM will force the daemon threads to terminate
User threads are created by the application. Mostly Daemon threads created by the JVM.
Mainly user threads are designed to do some specific task. Daemon threads are design as to support the user threads.
User threads are foreground threads. Daemon threads are background threads.
User threads are high priority threads. Daemon threads are low priority threads.
Its life independent. Its life depends on user threads.

示例:检查线程是否为守护进程

可以使用 setDaemon(boolean) 方法将用户线程设为守护线程。在这个例子中,使用 isDaemon() 方法检查线程类型(用户线程或守护线程)。如果它是守护进程则返回真,否则返回假。

// Java program check thread is Daemon or not
  
class MyThread extends Thread {
  
    @Override
    public void run()
    {
        System.out.println("User Thread or Non-Daemon Thread");
    }
}
  
class MainThread {
  
    public static void main(String[] args)
    {
  
        MyThread mt = new MyThread();
        mt.start();
  
        System.out.println("Main Thread");
  
        System.out.println("Is " + mt.getName()
                           + " a Daemon Thread: "
                           + mt.isDaemon());
  
        System.out.println("Is " + Thread.currentThread().getName()
                           + " a Daemon Thread: "
                           + Thread.currentThread().isDaemon());
    }
}
输出:
Main Thread
Is Thread-0 a Daemon Thread: false
Is main a Daemon Thread: false
User Thread or Non-Daemon Thread

示例:将非守护线程设为守护线程:
在此示例中,使用 setDeamon(boolean) 将非守护程序线程设为守护程序。

// Java program make user thread as a daemon thread
  
class MyThread extends Thread {
  
    @Override
    public void run()
    {
        System.out.println("Non-Daemon thread");
    }
}
  
class MainThread {
  
    public static void main(String[] args)
    {
        MyThread mt = new MyThread();
        System.out.println("Before using setDaemon() method: ");
        System.out.println("Is " + mt.getName()
                           + " a Daemon Thread: "
                           + mt.isDaemon());
  
        mt.setDaemon(true);
  
        System.out.println("After using setDaemon() method: ");
        System.out.println("Is " + mt.getName()
                           + " a Daemon Thread: "
                           + mt.isDaemon());
    }
}
输出:
Before using setDaemon() method: 
Is Thread-0 a Daemon Thread: false
After using setDaemon() method: 
Is Thread-0 a Daemon Thread: true