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中的線程組的詳細內容。更多資訊請關注PHP中文網其他相關文章!