java中的ThreadGroup可以定义为作为一个单元而创建的线程的集合,java中的ThreadGroup一般用于需要对一组线程执行组合操作的情况; ThreadGroup 提供了一种管理多个线程的有效方法。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
以下是 Java 中如何创建和使用 ThreadGroup 的语法。
package <packagename>; public class <mainclassname>{ public static void main(String args[]){ // creating threadgroup object ThreadGroup grp= new ThreadGroup(“<parent thread name>”); // creating threads belonging to given thread group <ThreadClassName> one =new <ThreadClassName>(“<threadname1>”, grp); // creating first member of thread group <ThreadClassName> two =new <ThreadClassName>(“<threadname2>”, grp); // creating second member of thread group } } class <ThreadClassName> extends Thread{ <ThreadClassName>(String threadName, ThreadGroup grp){ super(threadName,grp); // call to parent class constructor start(); // start the thread } public void run(){ // implement required logic } }
在上面的语法中,我们创建了一个 ThreadGroup 实例,并创建了两个属于线程组的线程。我们有一个主类和一个线程类,;是父线程名称。
以下是 java.lang.ThreadGroup 中可用构造函数的描述:
Constructor | Description |
public ThreadGroup(String name) | This constructor is used for creating a new thread group, the parent of this thread group is the same as the parent group of the currently executing thread. This constructor may throw SecurityException in case the currently running thread does not have permission to create a thread group. |
public ThreadGroup(ThreadGroupparentgroup,String name) | This constructor creates a thread group with a specified thread group as a parent and specified name. This constructor may throw NullPointerException in case the specified thread group is null, and SecurityException may be thrown in case the currently running thread does not have permission to create a thread group. |
Method | Description |
int activeCount() | This method returns the number of active running threads available in a given thread group. |
int activeGroupCount() | This method returns the number of active thread groups running. |
destroy() | This method destroys the thread group and its sub groups if available. |
int enumerate(Thread arr[]) | Call to this method puts the thread available in invoking thread group into the group array of threads. |
int enumerate(Thread arr[], boolean recurse) | Call to this method puts the thread available in invoking thread group into the group array of threads; if the recursive flag is true, then threads in subgroups are also added to the group. |
int enumerate(ThreadGroup[] thgrp) | This method puts the subgroups of the invoking thread group into the thread group array. |
int enumerate(ThreadGroup[] thgrp, boolean recursive) | This method puts the subgroups of the invoking thread group into the thread group array; if the recursive flag is set to true, then all subgroups of subgroups are added to the group array. |
以下是 java.lang.ThreadGroup 中可用的一些重要方法的列表:
描述
Java 中的 ThreadGroup 示例
下面给出的是 Java 中 ThreadGroup 的示例: 示例
package com.educba.threadgroupdemo; import java.lang.*; class ThreadDemo extends Thread { ThreadDemo(String threadname, ThreadGroup thgrp) { super(thgrp, threadname); start(); } public void run() { // implement required logic inside run method for (int i = 0; i < 2000; i++) { try { Thread.sleep(20); } catch (InterruptedException e) { System.out.println("InterruptedException Exception encountered"); } } } } public class ThreadGroupDemo { public static void main(String args[]) { // creating the thread group ThreadGroup grp = new ThreadGroup("parent-thread"); // creating new thread and adding to thread group ThreadDemo t1 = new ThreadDemo("first", grp); System.out.println("Starting first thread"); // creating another thread and adding to thread group ThreadDemo t2 = new ThreadDemo("two", grp); System.out.println("Starting second thread"); // finding the number of active threads System.out.println("Number of active threads running in thread group: " + grp.activeCount()); } }这里我们来看看java中如何使用线程组。
代码:
结论 上面的文章清楚地解释了java中线程组的工作;它是管理多个线程的内置方法。上面的java代码示例描述了如何在java应用程序中使用线程组。
以上是Java中的线程组的详细内容。更多信息请关注PHP中文网其他相关文章!