Java教程

Java isDaemon()方法

线程类的 isDaemon()方法检查该线程是否为守护线程。如果线程是守护程序线程,则此方法将返回true,否则返回false。

语法

public final boolean isDaemon()

返回

如果线程是守护程序线程,则此方法将返回true,否则返回false。

示例

public class JavaIsDaemonExp extends Thread
{
    public void run()
    {
        //checking for daemon thread
        if(Thread.currentThread().isDaemon())
        {
            System.out.println("daemon thread work");
        }
        else
        {
            System.out.println("user thread work");
        }
    }
    public static void main(String[] args)
    {
        // creating three threads
        JavaIsDaemonExp t1=new JavaIsDaemonExp();
        JavaIsDaemonExp t2=new JavaIsDaemonExp();
        JavaIsDaemonExp t3=new JavaIsDaemonExp();
        // set user thread t1 to daemon thread
        t1.setDaemon(true);
        //starting all the threads
        t1.start();
        t2.start();
        t3.start();
    }
}
输出:
daemon thread work
user thread work
user thread work

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4