首页 > Java > java教程 > 正文

Java中的线程组

王林
发布: 2024-08-30 16:07:45
原创
355 人浏览过

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 实例,并创建了两个属于线程组的线程。我们有一个主类和一个线程类,;是父线程名称。

ThreadGroup 在 Java 中如何工作?

  • 线程组是多个相关线程的集合。线程组允许开发人员同时处理多个java线程,并且在java.lang包中可用。
  • 内部线程组可以被认为是一棵树,其中每个线程都有一个父线程,除了没有与其关联的父线程的父线程。
  • 需要注意的是,属于特定线程组的线程可以访问该线程所属同一线程组的信息;它没有任何有关其父线程组或任何其他线程组的信息。

以下是 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.
公共线程组(字符串名称) 该构造函数用于创建一个新的线程组,该线程组的父级与当前正在执行的线程的父级组相同。如果当前运行的线程没有创建线程组的权限,则此构造函数可能会抛出 SecurityException。 公共线程组(线程组父组,字符串名称) 此构造函数创建一个以指定线程组为父级并指定名称的线程组。如果指定的线程组为null,则该构造函数可能会抛出NullPointerException;如果当前运行的线程没有创建线程组的权限,则可能会抛出SecurityException。 表>

以下是 java.lang.ThreadGroup 中可用的一些重要方法的列表:

方法

描述

int activeCount() 此方法返回给定线程组中可用的活动运行线程的数量。 int activeGroupCount() 此方法返回正在运行的活动线程组的数量。 销毁() 此方法会销毁线程组及其子组(如果可用)。 int enumerate(Thread arr[]) 调用此方法会将可用于调用线程组的线程放入线程组数组中。 int enumerate(Thread arr[], 布尔递归) 调用该方法会将调用线程组中可用的线程放入线程组数组中;如果递归标志为 true,则子组中的线程也会添加到组中。 int enumerate(ThreadGroup[] thgrp) 该方法将调用线程组的子组放入线程组数组中。 int enumerate(ThreadGroup[] thgrp, 布尔递归) 该方法将调用线程组的子组放入线程组数组中;如果递归标志设置为 true,则子组的所有子组都将添加到组数组中。 表>

除了上面列出的方法外,还有其他方法,可以根据需要使用。

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应用程序中使用线程组。

以上是Java中的线程组的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!