Java并发编程与技术内幕:ThreadGroup线程组应用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java并发编程与技术内幕:ThreadGroup线程组应用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4990字,纯文字阅读大概需要8分钟。
内容图文
package com.xinboedu.www.test; /* * * 功能概要: * * @author linbingwen * @since 2016年6月11日 */ public class ThreadGroupDemo { public static void main(String[] args) { ThreadGroup threadGroup1 = // 这是匿名类写法new ThreadGroup("group1") { // 继承ThreadGroup并重新定义以下方法 // 在线程成员抛出unchecked exception // 会执行此方法publicvoid uncaughtException(Thread t, Throwable e) { System.out.println(t.getName() + ": " + e.getMessage()); } }; // 这是匿名类写法 Thread thread1 = // 这个线程是threadGroup1的一员new Thread(threadGroup1, new Runnable() { publicvoid run() { // 抛出unchecked异常thrownew RuntimeException("测试异常"); } }); thread1.start(); } }
package com.xinboedu.www.test; import java.util.Date; import java.util.Random; import java.util.concurrent.TimeUnit; class Result { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } publicclass SearchTask implements Runnable { public SearchTask(Result result) { this.result = result; } private Result result; @Override publicvoid run() { String name = Thread.currentThread().getName(); System.out.println("Thread Start " + name); try { doTask(); result.setName(name); } catch (InterruptedException e) { System.out.printf("Thread %s: Interrupted\n", name); return; } System.out.println("Thread end " + name); } privatevoid doTask() throws InterruptedException { Random random = new Random((new Date()).getTime()); int value = (int) (random.nextDouble() * 100); // System.out.println("value="+value); System.out.printf("Thread %s: %d\n", Thread.currentThread().getName(), value); TimeUnit.SECONDS.sleep(value); } publicstaticvoid main(String[] args) { System.out.println("main thread start:"); //创建5个线程,并入group里面进行管理 ThreadGroup threadGroup = new ThreadGroup("Searcher"); Result result = new Result(); SearchTask searchTask = new SearchTask(result); for (int i = 0; i < 5; i++) { Thread thred = new Thread(threadGroup, searchTask); thred.start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } //通过这种方法可以看group里面的所有信息 System.out.printf("Number of Threads: %d\n", threadGroup.activeCount()); System.out.printf("Information about the Thread Group\n"); threadGroup.list(); //这样可以复制group里面的thread信息 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()); } waitFinish(threadGroup); //将group里面的所有线程都给interpet threadGroup.interrupt(); System.out.println("main thread end:"); } privatestaticvoid waitFinish(ThreadGroup threadGroup) { while (threadGroup.activeCount() > 0) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } }
转自:https://blog.csdn.net/Evankaka/article/details/51627380
package com.xinboedu.www.test; import java.util.Date;import java.util.Random;import java.util.concurrent.TimeUnit; class Result {private String name; public String getName() {return name;} public void setName(String name) {this.name = name;}} public class SearchTask implements Runnable { public SearchTask(Result result) {this.result = result;} private Result result; @Overridepublic void run() {String name = Thread.currentThread().getName();System.out.println("Thread Start " + name);try {doTask();result.setName(name);} catch (InterruptedException e) {System.out.printf("Thread %s: Interrupted\n", name);return;}System.out.println("Thread end " + name);} private void doTask() throws InterruptedException {Random random = new Random((new Date()).getTime());int value = (int) (random.nextDouble() * 100);//System.out.println("value="+value);System.out.printf("Thread %s: %d\n", Thread.currentThread().getName(),value);TimeUnit.SECONDS.sleep(value);} public static void main(String[] args) {System.out.println("main thread start:");//创建5个线程,并入group里面进行管理ThreadGroup threadGroup = new ThreadGroup("Searcher");Result result = new Result();SearchTask searchTask = new SearchTask(result);for (int i = 0; i < 5; i++) {Thread thred = new Thread(threadGroup, searchTask);thred.start();try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}//通过这种方法可以看group里面的所有信息System.out.printf("Number of Threads: %d\n", threadGroup.activeCount());System.out.printf("Information about the Thread Group\n");threadGroup.list(); //这样可以复制group里面的thread信息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());}waitFinish(threadGroup);//将group里面的所有线程都给interpetthreadGroup.interrupt();System.out.println("main thread end:");} private static void waitFinish(ThreadGroup threadGroup) {while (threadGroup.activeCount() > 0) {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}} }
原文:https://www.cnblogs.com/xyyou/p/12152472.html
内容总结
以上是互联网集市为您收集整理的Java并发编程与技术内幕:ThreadGroup线程组应用全部内容,希望文章能够帮你解决Java并发编程与技术内幕:ThreadGroup线程组应用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。