- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包额外内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java - ThreadGroup destroy() 方法
描述
Java ThreadGroup destroy() 方法销毁该线程组及其所有子组。此线程组必须为空,表示所有曾在此线程组中的线程都已停止。
声明
以下是java.lang.ThreadGroup.destroy() 方法的声明
public final void destroy()
参数
无
返回值
此方法不返回任何值。
异常
IllegalThreadStateException − 如果线程组不为空,或者线程组已被销毁。
SecurityException − 如果当前线程无法修改此线程组。
销毁单个ThreadObject对象的示例
以下示例展示了在单个ThreadGroup对象的情况下使用ThreadGroup destroy()方法的情况。我们创建了一个ThreadGroup对象并为其命名。然后,我们使用前面创建的threadgroup对象创建了两个线程。使用destroy()方法,一旦线程执行完毕,我们就销毁ThreadGroup对象。
package com.tutorialspoint; public class ThreadGroupDemo implements Runnable { public static void main(String[] args) { ThreadGroupDemo tg = new ThreadGroupDemo(); tg.start(); } public void start() { try { // create a ThreadGroup ThreadGroup threadGroup = new ThreadGroup("ThreadGroup"); // create a thread Thread t1 = new Thread(threadGroup, this); System.out.println("Starting " + t1.getName() + "..."); t1.start(); // create another thread Thread t2 = new Thread(threadGroup, this); System.out.println("Starting " + t2.getName() + "..."); t2.start(); // block until the other threads finish t1.join(); t2.join(); // destroy the ThreadGroup. threadGroup.destroy(); System.out.println(threadGroup.getName() + " destroyed." ); } catch (InterruptedException ex) { System.out.println(ex.toString()); } } // implements run() public void run() { for(int i = 0; i < 4;i++) { i++; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " finished executing."); } }
让我们编译并运行上述程序,这将产生以下结果:
Starting Thread-0... Starting Thread-1... Thread-0 finished executing. Thread-1 finished executing. ThreadGroup destroyed.
在多个ThreadGroup对象中销毁ThreadObject对象的示例
以下示例展示了在多个ThreadGroup对象的情况下使用ThreadGroup destroy()方法的情况。我们创建了一个ThreadGroup对象并为其命名。接下来,我们创建了一个子ThreadGroup对象。然后,我们使用前面创建的threadgroup对象创建了两个线程。使用destroy()方法,一旦线程执行完毕,我们就销毁ThreadGroup对象。
package com.tutorialspoint; public class ThreadGroupDemo implements Runnable { public static void main(String[] args) { ThreadGroupDemo tg = new ThreadGroupDemo(); tg.start(); } public void start() { try { // create a parent ThreadGroup ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup"); // create a child ThreadGroup for parent ThreadGroup ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup"); // create a thread Thread t1 = new Thread(pThreadGroup, this); System.out.println("Starting " + t1.getName() + "..."); t1.start(); // create another thread Thread t2 = new Thread(cThreadGroup, this); System.out.println("Starting " + t2.getName() + "..."); t2.start(); // block until the other threads finish t1.join(); t2.join(); // destroy the ThreadGroups. cThreadGroup.destroy(); System.out.println(cThreadGroup.getName() + " destroyed." ); pThreadGroup.destroy(); System.out.println(pThreadGroup.getName() + " destroyed." ); } catch (InterruptedException ex) { System.out.println(ex.toString()); } } // implements run() public void run() { for(int i = 0; i < 4;i++) { i++; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " finished executing."); } }
让我们编译并运行上述程序,这将产生以下结果:
Starting Thread-0... Starting Thread-1... Thread-0 finished executing. Thread-1 finished executing. Child ThreadGroup destroyed. Parent ThreadGroup destroyed.
在销毁ThreadObject对象时遇到异常的示例
以下示例展示了ThreadGroup destroy()方法的无效用法。我们创建了一个ThreadGroup对象并为其命名。接下来,我们创建了一个子ThreadGroup对象。然后,我们使用前面创建的threadgroup对象创建了两个线程。使用destroy()方法,我们尝试在线程执行之前销毁ThreadGroup对象。
package com.tutorialspoint; public class ThreadGroupDemo implements Runnable { public static void main(String[] args) { ThreadGroupDemo tg = new ThreadGroupDemo(); tg.start(); } public void start() { try { // create a parent ThreadGroup ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup"); // create a child ThreadGroup for parent ThreadGroup ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup"); // create a thread Thread t1 = new Thread(pThreadGroup, this); System.out.println("Starting " + t1.getName() + "..."); t1.start(); // create another thread Thread t2 = new Thread(cThreadGroup, this); System.out.println("Starting " + t2.getName() + "..."); t2.start(); // destroy the ThreadGroups. cThreadGroup.destroy(); System.out.println(cThreadGroup.getName() + " destroyed." ); pThreadGroup.destroy(); System.out.println(pThreadGroup.getName() + " destroyed." ); // block until the other threads finish t1.join(); t2.join(); } catch (InterruptedException ex) { System.out.println(ex.toString()); } } // implements run() public void run() { for(int i = 0; i < 4;i++) { i++; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " finished executing."); } }
让我们编译并运行上述程序,这将产生以下结果:
Starting Thread-0... Starting Thread-1... Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.ThreadGroup.destroy(Unknown Source) at com.tutorialspoint.ThreadGroupDemo.start(ThreadGroupDemo.java:28) at com.tutorialspoint.ThreadGroupDemo.main(ThreadGroupDemo.java:6) Thread-0 finished executing. Thread-1 finished executing.