免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.osgi.framework.Bundle;import org.osgi.framework.BundleEvent;import org.osgi.framework.BundleListener;import com.quickwebframework.entity.Log;import com.quickwebframework.entity.LogFactory;public abstract class ThreadContext private static Log log = LogFactory.getLog(ThreadContext.class); public static void init() final Bundle coreBundle = FrameworkContext.coreBundle; coreBundle.getBundleContext().addBundleListener(new BundleListener() Override public void bundleChanged(BundleEvent arg0) int eventType = arg0.getType(); Bundle bundle = arg0.getBundle(); / 如果插件的状态是正在停止或已经停止 if (eventType = BundleEvent.STOPPED | eventType = BundleEvent.STOPPING) if (bundle.equals(coreBundle) removeAllFilter(); else removeBundleAllThread(bundle); ); private static List threadList = new ArrayList(); private static MapBundle, List bundleThreadListMap = new HashMapBundle, List(); /* * 得到线程列表 * * return */ public static List getThreadList() return threadList; /* * 移除所有的线程 */ public static void removeAllFilter() for (Bundle bundle : bundleThreadListMap.keySet() .toArray(new Bundle0) removeBundleAllThread(bundle); /* * 移除某Bundle所有的线程 * * param bundle */ public static void removeBundleAllThread(Bundle bundle) if (!bundleThreadListMap.containsKey(bundle) return; Thread bundleThreadArray = bundleThreadListMap.get(bundle).toArray( new Thread0); for (Thread thread : bundleThreadArray) removeThread(bundle, thread); bundleThreadListMap.remove(bundle); /* * 移除线程 * * param bundle * param thread */ public static void removeThread(Bundle bundle, Thread thread) / 从Bundle对应的线程列表中移除 if (!bundleThreadListMap.containsKey(bundle) return; List bundleThreadList = bundleThreadListMap.get(bundle); bundleThreadList.remove(thread); / 从所有的线程列表中移除 threadList.remove(thread); String bundleName = bundle.getSymbolicName(); String threadName = String.format( Thread Id:%s ,Name:%s ,Class:%s ,Hashcode:%s, thread.getId(), thread.getName(), thread.getClass().getName(), Integer.toHexString(thread.hashCode(); try errupt(); log.debug(String.format(已成功向插件%s的线程%s发送中断命令!, bundleName, threadName); catch (Exception ex) log.warn(String.format(向插件%s的线程%s发送中断命令失败!, bundleName, threadName); ex.printStackTrace(); /* * 添加线程 * * param bundle * param thread */ public static void addThread(Bundle bundle, Thread thread) String threadClassName = thread.getClass().getName(); / 是否存在同类名实例 boolean hasSameClassNameObject = false; for (Thread preThread : threadList) if (preThread.getClass().getName().equals(threadClassName) hasSameClassNameObject = true; break; / 如果存在同类名实例,则抛出异常 if (hasSameClassNameObject) StringBuilder sb = new StringBuilder(); sb.append(String.format( 警告:将Bundle%s的线程类名:%s加入到ThreadContext中时,发现存在多个同类名实例!, bundle.getSymbolicName(), threadClassName); sb.append(n-同类名实例列表如下:); synchronized (bundleThreadListMap) for (Bundle tmpBundle : bundleThreadListMap.keySet() List tmpBundleThreadList = bundleThreadListMap .get(tmpBundle); for (Thread tmpThread : tmpBundleThreadList) if (tmpThread.getClass().getName() .equals(threadClassName) sb.append(String.format( n -Bundle%s,线程ID:%s ,类名:%s, tmpBundle.getSymbolicName(), tmpThread.getId(), threadClassName); String errorMessage = sb.toString(); log.warn(errorMessage); / 加入到Bundle对应的线程列表中 List bundleThreadList = null; if (bundleThreadListMap.containsKey(bundle) bundleThreadList = bundleThreadListMap.get(bundle); else bundleThreadList = new ArrayList(); bundleThreadListMap.put(bundle, bundleThreadList); bundleThreadList.add(thread); / 加入到全部线程列表中 threadList.add(thread); / 启动线程 t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 旅游文化展示系统项目分析方案
- 教育硬件设备采购项目分析方案
- 商务中心物业增值方案
- 物业客服标准化服务方案
- 医用注射器项目分析方案
- 智能建筑能耗管理系统开发分析方案
- 机器视觉检测系统搭建分析方案
- 轮胎智能胎压监测项目分析方案
- 物业社区志愿服务方案
- 物业保洁服务提升方案
- 中华人民共和国药品管理法
- 光伏运维安全规范
- 精准化课堂教学讲座课件
- 土石方分包管理暂行办法
- 失禁性皮炎护理病例分享
- 2025年湖南省普通高中学业水平合格性考试仿真试卷(专家版二)数学试题(含答案)
- QGDW10384-2023输电线路钢管塔加工技术规程
- 2025-2030年中国特种气体行业市场深度调研及发展战略与前景展望研究报告
- 发错药的不良事件讲课件
- 2025年陕西陕煤电力集团有限公司招聘笔试参考题库含答案解析
- 2025至2030中国智慧水务行业市场发展分析及前景趋势与投资价值报告
评论
0/150
提交评论