ホームページ  >  記事  >  Java  >  Javaのスレッドグループ

Javaのスレッドグループ

王林
王林オリジナル
2024-08-30 16:07:45356ブラウズ

Java の ThreadGroup は、ユニットとして動作するように作成されたスレッドのコレクションとして定義できます。Java の ThreadGroup は、通常、スレッドのグループに対して結合された操作を実行する必要がある場合に使用されます。 ThreadGroup は、複数のスレッドを管理する効率的な方法を提供します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

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 インスタンスを作成し、スレッド グループの一部である 2 つのスレッドを作成しました。メイン クラスとスレッド クラス、 があります。は親スレッドの名前です。

Java で ThreadGroup はどのように機能しますか?

  • スレッド グループは、複数の関連スレッドの集合です。スレッド グループを使用すると、開発者は複数の 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.
public ThreadGroup(文字列名) このコンストラクタは、新しいスレッド グループを作成するために使用されます。このスレッド グループの親は、現在実行中のスレッドの親グループと同じです。現在実行中のスレッドにスレッド グループを作成する権限がない場合、このコンストラクタは SecurityException をスローすることがあります。 public ThreadGroup(ThreadGroupparentgroup,String name) このコンストラクターは、指定されたスレッド グループを親として、指定された名前を持つスレッド グループを作成します。このコンストラクタは、指定されたスレッド グループが null の場合には NullPointerException をスローする可能性があり、現在実行中のスレッドにスレッド グループを作成する権限がない場合には SecurityException をスローする場合があります。 テーブル>

ここでは、java.lang.ThreadGroup で使用できるいくつかの重要なメソッドのリストを示します:

メソッド

説明

int activeCount() このメソッドは、指定されたスレッド グループで使用可能なアクティブな実行スレッドの数を返します。 int activeGroupCount() このメソッドは、実行中のアクティブなスレッド グループの数を返します。 destroy() このメソッドは、スレッド グループとそのサブグループ(使用可能な場合)を破棄します。 int enumerate(Thread arr[]) このメソッドを呼び出すと、スレッド グループの呼び出しで使用可能なスレッドがスレッドのグループ配列に配置されます。 int enumerate(Thread arr[], boolean recurse) このメソッドを呼び出すと、スレッド グループの呼び出しで使用可能なスレッドがスレッドのグループ配列に配置されます。再帰フラグが true の場合、サブグループ内のスレッドもグループに追加されます。 int enumerate(ThreadGroup[] thgrp) このメソッドは、呼び出し元のスレッド グループのサブグループをスレッド グループ配列に配置します。 int enumerate(ThreadGroup[] thgrp, boolean recursive) このメソッドは、呼び出し元のスレッド グループのサブグループをスレッド グループ配列に配置します。再帰フラグが 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。