Java并发编程实例--10.使用线程组

xiaoxiao2021-02-28  25

并发API提供的一个有趣功能是可以将多个线程组成一个组。

这样我们就能将这一组线程看做一个单元并且提供改组内线程对象的读取操作。例如

你有一些线程在执行同样的任务并且你想控制他们,不考虑有多少个线程仍在运行,一个打断动作将会打断所有组内线程的执行。 Java提供了一个ThreadGroup类来表示一组线程。一个线程组对象由多个线程对象和另一个线程组对象组成,从而形成树状的线程结构。

本例中,我们将学习使用ThreadGroup对象开发一个简单例子。我们有10个线程模拟搜索(随机睡眠一段时间),并且其中一个执行结束,我们将打断其余线程。

Result.java package com.dylan.thread.ch1.c10.task; /** * Class that stores the result of the search * */ public class Result { /** * Name of the Thread that finish */ private String name; /** * Read the name of the Thread * @return The name of the Thread */ public String getName() { return name; } /** * Write the name of the Thread * @param name The name of the Thread */ public void setName(String name) { this.name = name; } }

SearchTask.java package com.dylan.thread.ch1.c10.task; import java.util.Date; import java.util.Random; import java.util.concurrent.TimeUnit; /** * Class that simulates a search operation * */ public class SearchTask implements Runnable { /** * Store the name of the Thread if this Thread finish and is not interrupted */ private Result result; /** * Constructor of the class * @param result Parameter to initialize the object that stores the results */ public SearchTask(Result result) { this.result=result; } @Override public void run() { 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); } /** * Method that simulates the search operation * @throws InterruptedException Throws this exception if the Thread is interrupted */ private void doTask() throws InterruptedException { Random random=new Random((new Date()).getTime()); int value=(int)(random.nextDouble()*100); System.out.printf("Thread %s: %d\n",Thread.currentThread().getName(),value); TimeUnit.SECONDS.sleep(value); } }

Main.java package com.dylan.thread.ch1.c10.core; import com.dylan.thread.ch1.c10.task.Result; import com.dylan.thread.ch1.c10.task.SearchTask; import java.util.concurrent.TimeUnit; public class Main { /** * Main class of the example * @param args */ public static void main(String[] args) { // Create a ThreadGroup ThreadGroup threadGroup = new ThreadGroup("Searcher"); Result result=new Result(); // Create a SeachTask and 10 Thread objects with this Runnable SearchTask searchTask=new SearchTask(result); for (int i=0; i<10; i++) { Thread thread=new Thread(threadGroup, searchTask); thread.start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } // Write information about the ThreadGroup to the console System.out.printf("Number of Threads: %d\n",threadGroup.activeCount()); System.out.printf("Information about the Thread Group\n"); threadGroup.list(); // Write information about the status of the Thread objects to the console Thread[] threads=new Thread[threadGroup.activeCount()]; threadGroup.enumerate(threads); for (int i=0; i<threadGroup.activeCount(); i++) { System.out.printf("Thread %s: %s\n",threads[i].getName(),threads[i].getState()); } // Wait for the finalization of the Threadds waitFinish(threadGroup); // Interrupt all the Thread objects assigned to the ThreadGroup threadGroup.interrupt(); } /** * Method that waits for the finalization of one of the ten Thread objects * assigned to the ThreadGroup * @param threadGroup */ private static void waitFinish(ThreadGroup threadGroup) { while (threadGroup.activeCount()>9) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } }

运行结果:

Thread Thread-0: StartThread Thread-0: 59Thread Thread-1: StartThread Thread-1: 50Thread Thread-2: StartThread Thread-2: 29Thread Thread-3: StartThread Thread-3: 19Thread Thread-4: StartThread Thread-4: 47Thread Thread-5: StartThread Thread-5: 38Thread Thread-6: StartThread Thread-6: 66Thread Thread-7: StartThread Thread-7: 57Thread Thread-8: StartThread Thread-8: 83Thread Thread-9: StartThread Thread-9: 74Number 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-3: EndThread Thread-1: InterruptedThread Thread-8: InterruptedThread Thread-9: InterruptedThread Thread-0: InterruptedThread Thread-7: InterruptedThread Thread-6: InterruptedThread Thread-4: InterruptedThread Thread-2: InterruptedThread Thread-5: Interrupted

罗汉爷 认证博客专家 java 数据库管理员 全栈工程师 生活不会像你想象的那么好,也不会像你想象的那么糟。人的脆弱和坚强往往超乎了自己的想象,有时候会因为一句话就泪如雨下,而有时候发现自己艰难的走了很远。QQ: 2480035622Email: indexman@126.com网站:http://www.luohanye.com/
转载请注明原文地址: https://www.6miu.com/read-2629807.html

最新回复(0)