ThreadGroup類destroy()方法
- destroy() 方法在 java.lang 包中可用。
- destroy() 方法用於銷毀這個線程組,當我們銷毀這個線程組時,它的子線程組將同時被銷毀。
- destroy() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
-
destroy() 方法可能會在銷毀該線程組時拋出異常。
- SecurityException – 當不允許當前線程更新此線程組時,可能會拋出此異常。
- IllegalThreadStateException – 此異常可能在兩種情況下拋出
- 當這個線程組已經被銷毀時。
- 當此線程組不為空時。
- IllegalThreadStateException – 此異常可能在兩種情況下拋出
- SecurityException – 當不允許當前線程更新此線程組時,可能會拋出此異常。
用法:
public final void destroy();
參數:
- 它不接受任何參數。
返回值:
這個方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of void destroy() method of ThreadGroup
public class Destroy implements Runnable {
public static void main(String[] args) {
Destroy des = new Destroy();
des.destroyed();
}
public void destroyed() {
try {
// Create two thread group and the named are base
// and derived
ThreadGroup base = new ThreadGroup("Base ThreadGroup");
ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");
// create two threads
Thread th1 = new Thread(base, this);
Thread th2 = new Thread(derived, this);
//By using getName() method is to retrieve the
// name of the thread th1
System.out.println(th1.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th1
th1.start();
//By using getName() method is to retrieve the
// name of the thread th2
System.out.println(th2.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th2
th2.start();
System.out.println();
// By using join() method is to wait the current
// thread till complete execution of another
// thread
th1.join();
th2.join();
// By using destroy() method is to
// destroy the thread group named derived
derived.destroy();
boolean b1 = derived.isDestroyed();
System.out.println(derived.getName() + " " + "destroyed" + " " + b1);
boolean b2 = base.isDestroyed();
System.out.println(base.getName() + " " + "destroyed" + " " + b2);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// Override run()
public void run() {
for (int k = 0; k < 100; ++k)
++k;
System.out.println(Thread.currentThread().getName() + " " + "ends.....");
}
}
輸出
Thread-0 begins..... Thread-0 ends..... Thread-1 begins..... Thread-1 ends..... Derived ThreadGroup destroyed true Base ThreadGroup destroyed false
相關用法
- Java ThreadGroup enumerate()用法及代碼示例
- Java ThreadGroup getMaxPriority()用法及代碼示例
- Java ThreadGroup getParent()用法及代碼示例
- Java ThreadGroup getName()用法及代碼示例
- Java ThreadGroup parentOf()用法及代碼示例
- Java ThreadGroup interrupt()用法及代碼示例
- Java ThreadGroup suspend()用法及代碼示例
- Java ThreadGroup setDaemon()用法及代碼示例
- Java ThreadGroup isDaemon()用法及代碼示例
- Java ThreadGroup checkAccess()用法及代碼示例
- Java ThreadGroup resume()用法及代碼示例
- Java ThreadGroup isDestroyed()用法及代碼示例
- Java ThreadGroup toString()用法及代碼示例
- Java ThreadGroup list()用法及代碼示例
- Java ThreadGroup stop()用法及代碼示例
- Java ThreadGroup activeCount()用法及代碼示例
- Java ThreadGroup setMaxPriority()用法及代碼示例
- Java ThreadGroup activeGroupCount()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ThreadGroup destroy() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。