📜  Java多线程-threadgroup()线程组

📅  最后修改于: 2020-09-27 01:25:25             🧑  作者: Mango

Java中的线程组

Java提供了一种将多个线程组合到单个对象中的便捷方法。这样,我们可以通过单个方法调用来挂起,恢复或中断线程组。

注意:现在不建议使用suspend(),resume()和stop()方法。

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

ThreadGroup代表一组线程。一个线程组也可以包括另一个线程组。线程组创建一棵树,其中除初始线程组外的每个线程组都有一个父级。

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

ThreadGroup类的构造方法

ThreadGroup类只有两个构造函数。

No. Constructor Description
1) ThreadGroup(String name) creates a thread group with given name.
2) ThreadGroup(ThreadGroup parent, String name) creates a thread group with given parent group and name.

ThreadGroup类的方法

ThreadGroup类中有许多方法。下面列出了ThreadGroup方法。

S.N. Modifier and Type Method Description
1) void checkAccess() This method determines if the currently running thread has permission to modify the thread group.
2) int activeCount() This method returns an estimate of the number of active threads in the thread group and its subgroups.
3) int activeGroupCount() This method returns an estimate of the number of active groups in the thread group and its subgroups.
4) void destroy() This method destroys the thread group and all of its subgroups.
5) int enumerate(Thread[] list) This method copies into the specified array every active thread in the thread group and its subgroups.
6) int getMaxPriority() This method returns the maximum priority of the thread group.
7) String getName() This method returns the name of the thread group.
8) ThreadGroup getParent() This method returns the parent of the thread group.
9) void interrupt() This method interrupts all threads in the thread group.
10) boolean isDaemon() This method tests if the thread group is a daemon thread group.
11) void setDaemon(boolean daemon) This method changes the daemon status of the thread group.
12) boolean isDestroyed() This method tests if this thread group has been destroyed.
13) void list() This method prints information about the thread group to the standard output.
14) boolean parentOf(ThreadGroup g This method tests if the thread group is either the thread group argument or one of its ancestor thread groups.
15) void suspend() This method is used to suspend all threads in the thread group.
16) void resume() This method is used to resume all threads in the thread group which was suspended using suspend() method.
17) void setMaxPriority(int pri) This method sets the maximum priority of the group.
18) void stop() This method is used to stop all threads in the thread group.
19) String toString() This method returns a string representation of the Thread group.

让我们看一下将多个线程组合在一起的代码。

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接口的类,“ 1″,“ 2″和“ 3″是线程名称。

现在,我们只能用一行代码中断所有线程。

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]
    Thread[one,5,Parent ThreadGroup]
    Thread[two,5,Parent ThreadGroup]
    Thread[three,5,Parent ThreadGroup]