線程類的interrupted()方法用於檢查當前線程是否被中斷。此方法清除線程的中斷狀態,這意味著如果連續調用此方法兩次,第二次調用將返回 false。如果線程的中斷狀態為真,則此方法會將狀態設置為假。
用法
public static boolean interrupted()
返回
如果當前線程已被中斷,則此方法將返回 true,否則返回 false。
示例
public class JavaInterruptedExp extends Thread
{
public void run()
{
for(int i=1;i<=3;i++)
{
System.out.println("doing task....:"+i);
}
}
public static void main(String args[])throws InterruptedException
{
JavaInterruptedExp t1=new JavaInterruptedExp();
JavaInterruptedExp t2=new JavaInterruptedExp();
// call run() method
t1.start();
t2.start();
System.out.println("is thread t1 interrupted..:"+t1.interrupted());
// interrupt thread t1
t1.interrupt();
System.out.println("is thread t1 interrupted..:" +t1.interrupted());
System.out.println("is thread t2 interrupted..:"+t2.interrupted());
}
}
輸出:
is thread t1 interrupted..:false is thread t1 interrupted..:false is thread t2 interrupted..:false doing task....:1 doing task....:2 doing task....:3 doing task....:1 doing task....:2 doing task....:3
相關用法
- Java Thread interrupt()用法及代碼示例
- Java Thread isInterrupted()用法及代碼示例
- Java Thread isDaemon()用法及代碼示例
- Java Thread isAlive()用法及代碼示例
- 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 interrupted() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。