ThreadGroup 类的 destroy() 方法用于销毁线程组及其所有子组。线程组必须为空,表示该线程组中的所有线程都已停止。
用法:
public void destroy()
返回:
它不返回任何值。
异常:
IllegalThreadStateException:如果线程组不为空或线程组已被销毁,则抛出此异常。
SecurityException: 如果当前线程无法修改此线程组。
示例
class NewThread extends Thread
{
NewThread(String threadname, ThreadGroup tg)
{
super(tg, threadname);
}
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName() + " interrupted");
}
}
System.out.println(Thread.currentThread().getName() + " completed executing");
}
}
public class ThreadGroupDestroyExp
{
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
NewThread t1 = new NewThread("Thread-1", g1);
t1.start();
// creating another thread
NewThread t2 = new NewThread("Thread-2", g1);
t2.start();
// block until other thread is finished
t1.join();
t2.join();
// destroying child threadGroup
g2.destroy();
System.out.println(g2.getName() + " destroyed");
// destroying parent threadGroup
g1.destroy();
System.out.println(g1.getName() + " destroyed");
}
}
输出:
Thread-1 finished executing Thread-2 finished executing Child thread destroyed Parent thread destroyed
相关用法
- Java ThreadGroup destroy()用法及代码示例
- 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 ThreadGroup destroy() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。