ThreadGroup dalam java boleh ditakrifkan sebagai koleksi utas yang dicipta untuk berfungsi sebagai satu unit, ThreadGroup dalam java biasanya digunakan apabila terdapat keperluan untuk melakukan operasi gabungan pada kumpulan utas; ThreadGroup menawarkan cara yang cekap untuk mengurus berbilang rangkaian.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Sintaks:
Berikut ialah sintaks cara ThreadGroup dicipta dan digunakan dalam Java.
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 } }
Dalam sintaks di atas, kami telah mencipta contoh ThreadGroup dan mencipta dua utas yang merupakan sebahagian daripada kumpulan utas. Kami mempunyai kelas utama dan kelas utas,
Berikut ialah perihalan pembina yang tersedia dalam 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. |
Berikut ialah senarai beberapa kaedah penting yang terdapat dalam java.lang.ThreadGroup:
Penerangan
Contoh ThreadGroup dalam Java
Diberikan di bawah adalah contoh ThreadGroup dalam Java: Contoh
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()); } }
Kod:
Kesimpulan Artikel di atas menerangkan dengan jelas cara kerja kumpulan benang dalam java; ia ialah cara terbina untuk mengurus berbilang benang. Contoh kod java yang diterangkan di atas cara mengulirkan kumpulan digunakan dalam aplikasi java.
Atas ialah kandungan terperinci ThreadGroup dalam Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!