Java中的ThreadGroup

Java提供了一种方便的方法来将多个线程组合在一个对象中。通过单个方法调用,我们可以暂停、恢复或中断一组线程。

注意:现在suspend()、resume()和stop()方法已被弃用。

Java线程组是由java.lang.ThreadGroup类实现的。

ThreadGroup表示一组线程。线程组还可以包含其他线程组。线程组创建了一个树,除初始线程组外,每个线程组都有一个父线程组。

线程允许访问有关自己所在线程组的信息,但不能访问其线程组的父线程组或任何其他线程组的信息。

ThreadGroup类的构造方法

ThreadGroup类只有两个构造方法。

No.ConstructorDescription
1)线程组(字符串名称)创建一个具有给定名称的线程组。
2)线程组(线程组父级,字符串名称)创建具有给定父组和名称的线程组。

ThreadGroup类的方法

ThreadGroup类中有许多方法。下面是ThreadGroup方法的列表。

.Modifier and TypeMethodDescription
1)voidcheckAccess()该方法判断当前运行的线程是否有修改线程组的权限。
2)intactiveCount()此方法返回线程组及其子组中活动线程数的估计值。
3)intactiveGroupCount()此方法返回线程组及其子组中活动组数的估计值。
4)voiddestroy()此方法销毁线程组及其所有子组。
5)int[enumerate(Thread] list)此方法将线程组及其子组中的每个活动线程复制到指定的数组中。
6)intgetMaxPriority()此方法返回线程组的最大优先级。
7)StringgetName()此方法返回线程组的名称。
8)ThreadGroupgetParent()此方法返回线程组的父级。
9)voidinterrupt()该方法中断线程组中的所有线程。
10)booleanisDaemon()此方法测试线程组是否为守护线程组。
11)voidsetDaemon(boolean daemon)此方法更改线程组的守护程序状态。
12)booleanisDestroyed()此方法测试此线程组是否已被销毁。
13)voidlist()此方法将有关线程组的信息打印到标准输出。
14)booleanparentOf(ThreadGroup g此方法测试线程组是线程组参数还是其祖先线程组之一。
15)voidsuspend()该方法用于挂起线程组中的所有线程。
16)voidresume()此方法用于恢复使用 suspend() 方法挂起的线程组中的所有线程。
17)voidsetMaxPriority(int pri)此方法设置组的最大优先级。
18)voidstop()该方法用于停止线程组中的所有线程。
19)StringtoString()此方法返回线程组的字符串表示形式。

让我们看一个将多个线程分组的代码。

ThreadGroup tg1 = new ThreadGroup("Group A");   
Thread t1 = new Thread(tg1,new MyRunnable(),"one");     
Thread t2 = new Thread(tg1,new MyRunnable(),"two");     
Thread t3 = new Thread(tg1,new MyRunnable(),"three");    

现在,所有的3个线程都属于同一个线程组。这里,tg1是线程组名称,MyRunnable是实现Runnable接口的类,"one"、"two"和"three"是线程名称。

现在,我们可以通过一行代码中断所有的线程。

Thread.currentThread().getThreadGroup().interrupt();  

线程组示例

文件:ThreadGroupDemo.java

public class ThreadGroupDemo implements Runnable{    
    public void run() {    
          System.out.println(Thread.currentThread().getName());    
    }    
   public static void main(String[] args) {    
      ThreadGroupDemo runnable = new ThreadGroupDemo();    
          ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup");    
              
          Thread t1 = new Thread(tg1, runnable,"one");    
          t1.start();    
          Thread t2 = new Thread(tg1, runnable,"two");    
          t2.start();    
          Thread t3 = new Thread(tg1, runnable,"three");    
          t3.start();    
                 
          System.out.println("Thread Group Name: "+tg1.getName());    
         tg1.list();    
    
    }    
   }    

输出:

one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]

线程池方法示例:int activeCount()

让我们看一下如何使用activeCount()方法。

文件名:ActiveCountExample.java

// 说明 activeCount() 方法的代码   
  
// 导入语句 
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数 
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖运行方法   
public void run()  
{  
  
for (int j = 0; j < 1000; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered " + e);  
}  
}  
}  
}  
  
public class ActiveCountExample  
{  
// 主要方法   
public static void main(String argvs[])  
{  
//创建线程组    
ThreadGroup tg = new ThreadGroup("The parent group of threads");  
  
ThreadNew th1 = new ThreadNew("first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("second", tg);  
System.out.println("Starting the second");  
  
// 通过调用 activeCount() 方法检查活动线程的数量  
System.out.println("The total number of active threads are: " + tg.activeCount());  
}  
}  

输出:

Starting the first
Starting the second
The total number of active threads are: 2

线程池方法示例:int activeGroupCount()

现在,我们将学习如何在代码中使用activeGroupCount()方法。

文件名:ActiveGroupCountExample.java

// 说明 activeGroupCount() 方法的 Java 代码    
  
//导入语句   
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数    
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法  
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered " + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class ActiveGroupCountExample  
{  
// 主要方法  
public static void main(String argvs[])  
{  
// 创建线程组    
ThreadGroup tg = new ThreadGroup("The parent group of threads");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
// 通过调用 activeGroupCount() 方法检查活动线程的数量
System.out.println("The total number of active thread groups are: " + tg.activeGroupCount());  
}  
}  

输出:

Starting the first
Starting the second
The total number of active thread groups are: 1
the second thread has finished executing
the first thread has finished executing

线程池方法示例:void destroy()

现在,我们将学习如何在代码中使用destroy()方法。

文件名:DestroyExample.java

// 说明 destroy() 方法的代码   
  
// 导入语句  
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数   
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法  
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered " + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class DestroyExample   
{  
// 主要方法 
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组 
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
// 等到其他线程完成  
th1.join();  
th2.join();  
  
// 销毁子线程组  
tg1.destroy();  
System.out.println(tg1.getName() + " is destroyed.");  
  
// 销毁父线程组  
tg.destroy();  
System.out.println(tg.getName() + " is destroyed.");  
}  
}  

输出:

Starting the first
Starting the second
the first thread has finished executing
the second thread has finished executing
the child group is destroyed.
the parent group is destroyed.

线程池方法示例:int enumerate()

现在,我们将学习如何在代码中使用enumerate()方法。

文件名:EnumerateExample.java

// 说明 enumerate() 方法的代码  
  
// 导入语句   
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数  
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法  
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered " + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class EnumerateExample   
{  
// 主要方法   
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组   
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
// 返回保存在此数组中的线程数   
Thread[] grp = new Thread[tg.activeCount()];  
int cnt = tg.enumerate(grp);  
for (int j = 0; j < cnt; j++)   
{  
System.out.println("Thread " + grp[j].getName() + " is found.");  
}  
}  
}  

输出:

Starting the first
Starting the second
Thread the first is found.
Thread the second is found.
the first thread has finished executing
the second thread has finished executing

线程池方法示例:int getMaxPriority()

以下代码展示了如何使用getMaxPriority()方法。

文件名:GetMaxPriorityExample.java

// 说明 getMaxPriority() 方法的代码  
  
// 导入语句  
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数  
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法  
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered " + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class GetMaxPriorityExample   
{  
// 主要方法   
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组   
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
int priority = tg.getMaxPriority();  
  
System.out.println("The maximum priority of the parent ThreadGroup: " + priority);  
  
  
}  
}  

输出:

Starting the first
Starting the second
The maximum priority of the parent ThreadGroup: 10
the first thread has finished executing
the second thread has finished executing

线程池方法示例:ThreadGroup getParent()

现在,我们将学习如何在代码中使用getParent()方法。

文件名:GetParentExample.java

// 说明 getParent() 方法的代码   
  
// 导入语句  
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
//类的构造函数   
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法  
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered" + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class GetMaxPriorityExample   
{  
// 主要方法   
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组  
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
// 打印父线程组     
// 子线程和父线程    
System.out.println("The ParentThreadGroup for " + tg.getName() + " is " + tg.getParent().getName());  
System.out.println("The ParentThreadGroup for " + tg1.getName() + " is " + tg1.getParent().getName());  
  
  
}  
}  

输出:

Starting the first
Starting the second
The ParentThreadGroup for the parent group is main
The ParentThreadGroup for the child group is the parent group
the first thread has finished executing
the second thread has finished executing

线程池方法示例:void interrupt()

以下程序演示了如何使用interrupt()方法。

文件名:InterruptExample.java

// 说明 interrupt() 方法的代码  
  
// 导入语句  
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数  
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法   
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered " + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class InterruptExample   
{  
//主要方法 
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组   
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
// 调用中断方法   
tg.interrupt();  
  
}  
}  

输出:

Starting the first
Starting the second
The exception has been encountered java.lang.InterruptedException: sleep interrupted
The exception has been encountered java.lang.InterruptedException: sleep interrupted
the second thread has finished executing
the first thread has finished executing

线程池方法示例:boolean isDaemon()

以下程序演示了如何使用isDaemon()方法。

文件名:IsDaemonExample.java

// 说明 isDaemon() 方法的代码 
  
// 导入语句  
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数    
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法  
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered" + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class IsDaemonExample   
{  
// 主要方法    
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组  
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
if (tg.isDaemon() == true)  
{  
System.out.println("The group is a daemon group.");  
}  
else  
{  
System.out.println("The group is not a daemon group.");  
}  
  
}  
}  

输出:

Starting the first
Starting the second
The group is not a daemon group.
the second thread has finished executing
the first thread has finished executing

线程池方法示例:boolean isDestroyed()

以下程序演示了如何使用isDestroyed()方法。

文件名:IsDestroyedExample.java

// 说明 isDestroyed() 方法的代码  
  
// 导入语句   
import java.lang.*;  
  
  
class ThreadNew extends Thread  
{  
// 类的构造函数  
ThreadNew(String tName, ThreadGroup tgrp)  
{  
super(tgrp, tName);  
start();  
}  
  
// 覆盖 run() 方法 
public void run()  
{  
  
for (int j = 0; j < 100; j++)  
{  
try  
{  
Thread.sleep(5);  
}  
catch (InterruptedException e)  
{  
System.out.println("The exception has been encountered" + e);  
}  
  
}  
  
System.out.println(Thread.currentThread().getName() + " thread has finished executing");  
}  
}  
  
public class IsDestroyedExample   
{  
// 主要方法    
public static void main(String argvs[]) throws SecurityException, InterruptedException  
{  
// 创建线程组  
ThreadGroup tg = new ThreadGroup("the parent group");  
  
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");  
  
ThreadNew th1 = new ThreadNew("the first", tg);  
System.out.println("Starting the first");  
  
ThreadNew th2 = new ThreadNew("the second", tg);  
System.out.println("Starting the second");  
  
if (tg.isDestroyed() == true)  
{  
System.out.println("The group has been destroyed.");  
}  
else  
{  
System.out.println("The group has not been destroyed.");  
}  
  
}  
}  

输出:

Starting the first
Starting the second
The group has not been destroyed.
the first thread has finished executing
the second thread has finished executing

标签: java, Java面试题, Java下载, java教程, java技术, Java学习, Java学习教程, Java语言, Java开发, Java入门教程, Java进阶教程, Java高级教程, Java笔试题, Java编程思想