首页  >  文章  >  Java  >  Java 中的守护线程

Java 中的守护线程

WBOY
WBOY原创
2024-08-30 16:03:14429浏览

java中的守护线程是优先级最低的线程,用于为线程提供服务,进而用于执行后台任务。在后台执行的任务包括垃圾收集和许多其他涉及 JVM 的任务,以防止其在退出后同时执行。 Java 虚拟机在发现其他实际上不是守护线程的用户线程时自行终止。如果 Java 虚拟机发现 Daemon 线程,那么它会先终止该线程,然后它自己会关闭。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

Daemon Thread 没有特定的语法,但可以作为 Daemon Thread 的一部分通过其他方法创建和调用,表示为:

public class DaemonThread extends Thread
  • public 类: 类的访问修饰符是 public 并且
  • DaemonThread:线程的类型是DaemonThread,因此它继承并扩展自Thread类。
  • Thread:线程类用于将Thread类的属性继承给Daemon线程。

此外,它还涉及各种方法,这些方法具有不同的语法来调用 Daemon 线程。

public final void setDaemon(boolean on)

此方法语法将参数作为布尔值传递,用于因为当守护线程标记为布尔值时,该线程是守护线程,这意味着 true。

public final boolean isDaemon()

如果找到的线程是守护线程,则此方法返回值 true;如果找到的线程为 false,则返回 false。

Java 中的守护线程是如何工作的?

守护线程是用于创建线程并继承其父线程的所有属性的线程。因此,主方法中存在的所有线程都称为子方法,子方法继承了父类的大部分执行属性并执行任何任务。它们也称为非守护线程。

这就是通过调用所有将线程设置为守护线程的方法将非守护线程或用户线程设置为守护线程的真实情况。可以使用 Thread 类的 setDaemon() 方法轻松将其设置为守护线程,该类还扩展了可运行接口。

需要记住的一个非常重要的一点是,每当 JVM 启动时,就会创建一个名为 main 的线程,并且任何程序都会在此时运行或启动,或者我们可以说这是执行流程开始的点。默认情况下,所有线程都将在此主线程上运行,直到启动新线程。如果启动新线程,则子线程将负责创建任何新方法。

然后是负责创建和确定守护线程的方法,其中涉及到setDaemon方法等方法,该方法会根据传递给该方法的参数值来确定创建的线程是守护线程还是非守护线程。

公共布尔线程 isDaemon() 另一方面用于根据返回类型检查线程的状态;如果为 false,则该线程是非守护线程;否则,它是守护进程。

方法

以下是下面提到的方法

1. public void setDaemon(布尔状态)

该方法用于根据传入的参数判断当前线程执行为守护线程,如果传入的参数为boolean,则该线程为守护线程;如果传入的参数不为boolean,则该线程为非守护线程.

它还捕获异常,例如 IllegalThreadStateException 或 Security Exception。如果确定要捕获非法ThreadStateException,那么先决条件是该线程必须仅处于活动状态。如果当前线程无法修改,则会使用Security Exception。

2. public boolean isDaemon()

该方法用于验证当前情况或者根据返回类型说出当前状态;如果返回类型为true,那么它是一个守护线程;否则,它是非守护线程。

Java 中守护线程的示例

以下是下面提到的示例:

示例#1

本程序演示了使用 void setDaemon(boolean status) 将线程设置为守护线程以进行进一步操作的方法,以及在识别守护线程后调用一次 isDaemon() 方法,该方法表示该线程集是守护线程还是守护线程非守护线程取决于参数设置的布尔值。

代码:

public class Daemon_Thread_Demo_set extends Thread {
public Daemon_Thread_Demo_set(String nm){
super(nm);
}
public void run()
{
if(Thread.currentThread().isDaemon())
{
System.out.println("Current_Thread is a Daemon thread" + Thread.currentThread().getName());
}
else
{
System.out.println("Current thread is a non-daemon thread. "+ Thread.currentThread().getName());
}
}
public static void main(String[] args) {
Daemon_Thread_Demo_set nm1 = new Daemon_Thread_Demo_set("nm1");
Daemon_Thread_Demo_set nm2 = new Daemon_Thread_Demo_set("nm2");
Daemon_Thread_Demo_set nm3 = new Daemon_Thread_Demo_set("nm3");
nm1.setDaemon(true);
nm1.start();
nm2.start();
nm3.setDaemon(false);
nm3.start();
}
}

输出:

Java 中的守护线程

Example #2

This program demonstrates the illegal exceptions which are thrown once the current thread is identified as the Daemon thread but then the value when the set is not coming out to be set value matched with the thread it will be throwing the illegal exception as represented in the output.

Code:

public class Daemon_thrd_demo2 extends Thread {
public void run()
{
System.out.println("Current_thread_Name: "+Thread.currentThread().getName());
System.out.println("thread_as_Daemon"+Thread.currentThread().isDaemon());
}
public static void main(String[] args) {
Daemon_thrd_demo2 tst1 = new Daemon_thrd_demo2();
Daemon_thrd_demo2 tst2 = new Daemon_thrd_demo2();
tst1.start();
tst1.setDaemon(true);
tst2.start();
}
}

Output:

Java 中的守护线程

Note: The above program gives the Exception because the user thread should be set as a daemon thread only before calling the setDaemon() method. If it is not called in this way, then the thread after invocation will throw these exceptions while execution. Also, all non-daemon thread and daemon thread should be set up keeping in mind the place, i.e. before the child class or before the parent class.

Conclusion

Daemon thread and non-daemon thread have differences in them. It is very important to identify these differences as it can lead to conflicts and exception because daemon thread is a kind of thread that has the lowest priority compared to other threads while execution.

以上是Java 中的守护线程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java thread dump下一篇:Java Thread Pool