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類
下一篇:無