The Java API provides an interesting feature that enables you to group threads. This allows us to treat a group of threads as a single unit, accessing and manipulating the group's inline objects. For example, for some threads that perform the same tasks, you want to control them, no matter how many threads are running, only one single call is required, and all of these threads will be interrupted.
Java provides a Threadgroup class that represents a set of threads. A thread group can contain thread objects, or it can contain other thread group objects, which are a tree-shaped structure.
Here we learn to use the Threadgroup object class to develop a simple example: create 10 threads and have them hibernate for a random time (such as simulating a query), and when one of the threads looks successful, we will break the other 9 threads.
1. Create a class named result
Public class Result { private String name; Public String GetName () { return name; } Public void setName (String name) { this. Name = name; } }
2. Create a class named Searchtask
Importjava.util.Date;ImportJava.util.Random;ImportJava.util.concurrent.TimeUnit; Public classSearchtaskImplementsRunnable {Privateresult result; Publicsearchtask (Result result) { This. result =result; } @Override Public voidrun () {String name=Thread.CurrentThread (). GetName (); System.out.printf ("Thread%s:start\n", name); Try{dotask (); Result.setname (name); } Catch(interruptedexception e) {System.out.printf ("Thread%s:interrupted\n", name); return; } System.out.printf ("Thread%s:end\n", name); } Private voidDotask ()throwsinterruptedexception{Random R=NewRandom ((NewDate ()). GetTime ()); intValue = (int) (R.nextdouble () *100); System.out.printf ("Thread%s:%d\n", Thread.CurrentThread (). GetName (), value); TimeUnit.SECONDS.sleep (value); } }
3. Create the main class main
ImportJava.util.concurrent.TimeUnit; Public classMain { Public Static voidMain (string[] args) {//Create a thread groupThreadgroup Threadgroup =NewThreadgroup ("Searcher"); Result result=NewResult (); Searchtask St=Newsearchtask (Result); Try { for(inti=0;i<10;i++) {Thread T=NewThread (Threadgroup, ST); T.start (); TimeUnit.SECONDS.sleep (1); } } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.printf ("Number of Threads:%d\n", Threadgroup.activecount ()); System.out.printf ("Information about the Thread group\n"); Threadgroup.list (); //Print Thread Group informationthread[] Threads =NewThread[threadgroup.activecount ()]; Threadgroup.enumerate (threads); for(intI=0;i<threadgroup.activecount (); i++) {System.out.printf ("Thread%s:%s\n", Threads[i].getname (), threads[i].getstate ()); } //wait until the first thread of a thread group runs to end Try { while(Threadgroup.activecount () >9) {TimeUnit.SECONDS.sleep (1); } } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } //other threads in the thread groupThreadgroup.interrupt (); }}
4. The program operation results are as follows
Thread Thread-0: Startthread Thread-0:75Thread Thread-1: Startthread Thread-1:11Thread Thread-2: Startthread Thread-2:99Thread Thread-3: Startthread Thread-3:27Thread Thread-4: Startthread Thread-4:18Thread Thread-5: Startthread Thread-5:46Thread Thread-6: Startthread Thread-6:37Thread Thread-7: Startthread Thread-7:18Thread Thread-8: Startthread Thread-8:6Thread Thread-9: Startthread Thread-9:34Number of Threads:10information about the Thread groupjava.lang.threadgroup[name=searcher,maxpri=10] Thread[thread-0,5, Searcher] Thread[thread-1,5, Searcher] Thread[thread-2,5, Searcher] Thread[thread-3,5, Searcher] Thread[thread-4,5, Searcher] Thread[thread-5,5, Searcher] Thread[thread-6,5, Searcher] Thread[thread-7,5, Searcher] Thread[thread-8,5, Searcher] Thread[thread-9,5, Searcher] Thread Thread-0: Timed_waitingthread Thread-1: Timed_waitingthread Thread-2: Timed_waitingthread Thread-3: Timed_waitingthread Thread-4: Timed_waitingthread Thread-5: Timed_waitingthread Thread-6: Timed_waitingthread Thread-7: Timed_waitingthread Thread-8: Timed_waitingthread Thread-9: Timed_waitingthread Thread-1: Endthread Thread-3: Interruptedthread Thread-2: Interruptedthread Thread-0: Interruptedthread Thread-4: Interruptedthread Thread-6: Interruptedthread Thread-5: Interruptedthread Thread-8: Interruptedthread Thread-7: Interruptedthread Thread-9:interrupted
Grouping of threads