線程類的 destroy() 方法用於銷毀線程組及其所有子組。線程組必須為空,表示該線程組中的所有線程都已停止。
用法
public void destroy()
返回
它不返回任何值。
異常
IllegalThreadStateException:如果線程組不為空或線程組已被銷毀,則拋出此異常。
SecurityException: 如果當前線程無法修改此線程組。
示例
public class JavaDestroyExp extends Thread
{
JavaDestroyExp(String threadname, ThreadGroup tg)
{
super(tg, threadname);
start();
}
public void run()
{
for (int i = 0; i < 2; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex) {
System.out.println("Exception encounterted");}
}
System.out.println(Thread.currentThread().getName() +
" finished executing");
}
public static void main(String arg[]) throws InterruptedException, SecurityException
{
// creating a ThreadGroup
ThreadGroup g1 = new ThreadGroup("Parent thread");
// creating a child ThreadGroup for parent ThreadGroup
ThreadGroup g2 = new ThreadGroup(g1, "child thread");
// creating a thread
JavaDestroyExp t1 = new JavaDestroyExp("Thread-1", g1);
// creating another thread
JavaDestroyExp t2 = new JavaDestroyExp("Thread-2", g1);
// block until other thread is finished
t1.join();
t2.join();
// destroying child thread
g2.destroy();
System.out.println(g2.getName() + " destroyed");
// destroying parent thread
g1.destroy();
System.out.println(g1.getName() + " destroyed");
}
}
輸出:
Thread-1 finished executing Thread-2 finished executing child thread destroyed Parent thread destroyed
相關用法
- Java Thread dumpStack()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread suspend()用法及代碼示例
- Java Thread holdLock()用法及代碼示例
- Java Thread getContextClassLoader()用法及代碼示例
- Java Thread setContextClassLoader()用法及代碼示例
- Java Thread sleep()用法及代碼示例
- Java Thread getThreadGroup()用法及代碼示例
- Java Thread isInterrupted()用法及代碼示例
- Java Thread enumerate()用法及代碼示例
- Java Thread notify()用法及代碼示例
- Java Thread resume()用法及代碼示例
- Java Thread activeCount()用法及代碼示例
- Java Thread isDaemon()用法及代碼示例
- Java Thread setDaemon()用法及代碼示例
- Java Thread getId()用法及代碼示例
- Java Thread setName()用法及代碼示例
- Java Thread getDefaultUncaughtExceptionHandler()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Thread destroy() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。