Java Thread isDaemon()方法

Thread类的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

上一篇: Java Runtime类 下一篇:无