已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ThreadGroup线程组应用一、基本方法1、获取当前线程组名java view plain copy 在CODE上查看代码片派生到我的代码片Thread.currentThread().getThreadGroup().getName() 在main方法是调用输出是:main2、将线程放入到一个线程组中去java view plain copy 在CODE上查看代码片派生到我的代码片ThreadGroup threadGroup1 = new ThreadGroup(group1); ThreadGroup threadGroup2 = new ThreadGroup(group2); Thread thread1 =new Thread(threadGroup1, group1s member); Thread thread2 =new Thread(threadGroup2, group2s member); 其中Thread中和ThreadGroup相关的构造函数:java view plain copy 在CODE上查看代码片派生到我的代码片 public Thread(ThreadGroup group, Runnable target) init(group, target, Thread- + nextThreadNum(), 0); public Thread(ThreadGroup group, String name) init(group, null, name, 0); public Thread(ThreadGroup group, Runnable target, String name) init(group, target, name, 0); public Thread(ThreadGroup group, Runnable target, String name, long stackSize) init(group, target, name, stackSize); 它们最终都是调用同一个函数:java view plain copy 在CODE上查看代码片派生到我的代码片 private void init(ThreadGroup g, Runnable target, String name, long stackSize) Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); if (g = null) /安全检查 if (security != null) g = security.getThreadGroup(); /设置线程组 if (g = null) g = parent.getThreadGroup(); /检查可达性 g.checkAccess(); /是否有权限访问 if (security != null) if (isCCLOverridden(getClass() security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION); /往线程组添加线程但未启动 g.addUnstarted(); this.group = g; this.daemon = parent.isDaemon();/是否守护线程 this.priority = parent.getPriority();/优先级 = name.toCharArray(); if (security = null | isCCLOverridden(parent.getClass() this.contextClassLoader = parent.getContextClassLoader(); else this.contextClassLoader = parent.contextClassLoader; this.inheritedAccessControlContext = AccessController.getContext(); this.target = target; setPriority(priority); if (parent.inheritableThreadLocals != null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); this.stackSize = stackSize; tid = nextThreadID(); this.me = this; 3、复制线程组:java view plain copy 在CODE上查看代码片派生到我的代码片/这样可以复制group里面的thread信息 Thread threads = new ThreadthreadGroup.activeCount(); threadGroup.enumerate(threads); 这里的activeCount很明显就是取得活动的线程,注意。默认情况 下,连同其子线程组也会进行复制。4、未捕获异常处理ThreadGroup中有一个uncaughtException()方法。当线程组中某个线程发生Unchecked exception异常时,由执行环境调用此方法进行相关处理,如果有必要,可以重新定义此方法二、应用实例1、实例应用java view plain copy 在CODE上查看代码片派生到我的代码片package com.func.axc.threadgroup; 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) = name; public class SearchTask implements Runnable public SearchTask(Result result) this.result = result; private Result result; Override public 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: Interruptedn, 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.printf(Thread %s: %dn, 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: %dn, threadGroup.activeCount(); System.out.printf(Information about the Thread Groupn); threadGroup.list(); /这样可以复制group里面的thread信息 Thread threads = new ThreadthreadGroup.activeCount(); threadG(threads); for (int i = 0; i 0) try TimeUnit.SECONDS.sleep(1); catch (InterruptedException e) e.printStackTrace(); 输出结果:2、统一异常处理实例java view plain copy 在CODE上查看代码片派生到我的代码片package com.func.axc.threadgroup; /* * 功能概要: * * author linbingwen * since 2016年6月11日 */ public class ThreadGroupDemo public static void main(String args) ThreadGroup threadGroup1 = / 这是匿名类写法 new ThreadGroup(group1) / 继承ThreadGroup并重新定义以下方法 / 在线程成员抛出unchecked exception / 会执行此方法 public void uncaughtException(Thread t, Throwable e) System.out.println(t.getName() + : + e.getMessage(); ; / 这是匿名类写法 Thread thread1 = / 这个线程是threadGroup1的一员 new Thread(threadGroup1, new Runnable() public void run() / 抛出unchecked异常 throw new RuntimeException(测试异常); ); thread1.start(); 三、源码解读首先看其包含的变量java view plain copy 在CODE上查看代码片派生到我的代码片public class ThreadGroup implements Thread.UncaughtExceptionHandler private final ThreadGroup parent;/父亲ThreadGroup String name;/ThreadGroupr 的名称 int maxPriority;/线程最大优先级 boolean destroyed;/是否被销毁 boolean daemon;/是否守护线程 boolean vmAllowSuspension;/是否可以中断 int nUnstartedThreads = 0;/还未启动的线程 int nthreads;/ThreadGroup中线程数目 Thread threads;/ThreadGroup中的线程 int ngroups;/线程组数目 ThreadGroup groups;/线程组数组 从源码中可以看出,其包含的变量并不是很多。这里需要注意(1)线程组也可以包含其他线程组。如上面的groups.(2)线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组构造函数:java view plain copy 在CODE上查看代码片派生到我的代码片/私有构造函数 private ThreadGroup() = system; this.maxPriority = Thread.MAX_PRIORITY; this.parent = null; /默认是以当前ThreadGroup传入作为parent ThreadGroup,新线程组的父线程组是目前正在运行线程的线程组。 public ThreadGroup(String name) this(Thread.currentThread().getThreadGroup(), name); /构造函数 public ThreadGroup(ThreadGroup parent, String name) this(checkParentAccess(parent), parent, name); /私有构造函数 private ThreadGroup(Void unused, ThreadGroup parent, String name) = name; this.maxPriority = parent.maxPriority; this.daemon = parent.daemon; this.vmAllowSuspension = parent.vmAllowSuspension; this.parent = parent; parent.add(this); 其终的调用构造函数只有一个,父线程组的 checkAccess 方法在checkParentAccess中会调用:java view plain copy 在CODE上查看代码片派生到我的代码片/检查parent ThreadGroup private static Void checkParentAccess(ThreadGroup parent) parent.checkAccess(); return null; 未捕获异常设置:java view plain copy 在CODE上查看代码片派生到我的代码片public void uncaughtException(Thread t, Throwable e) if (parent != null) parent.uncaughtException(t, e);/父线程组不为空,设置到父线程组 else Thread.UncaughtExceptionHandler ueh = Thread.getDefaultUncaughtExceptionHandler(); if (ueh != null) ueh.uncaughtException(t, e); else if (!(e instanceof ThreadDeath) System.err.print(Exception in thread + t.getName() + ); e.printStackTrace(System.err); 如果父线程组存在, 则调用它的uncaughtException方法.如果父线程组不存在, 但指定了默认处理器 (下节中的As the default handler for the application), 则调用默认的处理器如果默认处理器没有设置, 则写错误日志.但如果 exception是ThreadDeath实例的话, 忽略线程组复制:java view plain copy 在CODE上查看代码片派生到我的代码片/此线程组及其子组中的所有活动线程复制到指定数组中。 public int enumerate(ThreadGroup list) checkAccess(); return enumerate(list, 0, true); /此线程组及其子组中的所有活动线程复制到指定数组中。 public int enu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 互联网营销师记录考核试卷含答案
- 名贵钟表鉴定师竞争分析模拟考核试卷含答案
- 广东省深圳市2024-2025学年八年级上学期期末道德与法治试题(含答案)
- 履带运输车司机安全专项评优考核试卷含答案
- 2026年DVD电池行业洞察报告及未来五至十年发展趋势报告
- 2025-2026浙江版五年级下道德法治练习卷
- 2025-2026华东师大高中三年级语文下册期末卷
- 2025-2026鲁教版高二政上学期末试卷
- 2026年政治学国际政治国际法试题及答案
- 2026年音乐学专业音乐评估测试试卷及答案
- 2026新版动火作业专题培训课件
- 兴国县江背河山洪沟防洪治理工程水土保持报告书
- 小学数学分层教学设计实践案例
- 珠江流域(片)水土保持规划
- 学前儿童家庭与社区教育(第2版)课件 第0-6章 绪论、家庭-家庭与儿童情绪能力的发展
- 纪委内部信息报送制度
- 充电桩安全检测仪器使用操作规范
- 《极限配合与技术测量基础(第六版)》中职全套教学课件
- 儿童生长曲线动态监测与生长发育心理行为干预
- 管道水压试验培训课件
- 摄影摄像课教学说课稿模板
评论
0/150
提交评论