線程類的 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 Thread isInterrupted()用法及代碼示例
- Java Thread isAlive()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
- Java Thread interrupt()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread suspend()用法及代碼示例
- Java Thread destroy()用法及代碼示例
- Java Thread holdLock()用法及代碼示例
- Java Thread getContextClassLoader()用法及代碼示例
- Java Thread setContextClassLoader()用法及代碼示例
- Java Thread sleep()用法及代碼示例
- Java Thread getThreadGroup()用法及代碼示例
- Java Thread enumerate()用法及代碼示例
- Java Thread notify()用法及代碼示例
- Java Thread resume()用法及代碼示例
- Java Thread activeCount()用法及代碼示例
- Java Thread setDaemon()用法及代碼示例
- Java Thread getId()用法及代碼示例
- Java Thread setName()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Thread isDaemon() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。