首頁 > 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學習者快速成長!