免费预览已结束,剩余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-2026年大学英语四级听力专项练习题
- 2025-2026年老年照护技能考核模拟试卷
- 某纺织厂质量追溯规范
- 船舶厂质量管理体系办法
- 机械加工质量制度
- 功率监测合同
- 九年级下册英语外研版Module 8 My future life
- 家庭教育工作总结(2篇)
- 小学道法教资面试历年真题题库
- 高中数学教资面试教资面试历年真题题库
- 武汉市2027届高中毕业生九月调研考试物理试卷(含答案及解析)
- 2026年南昌县塘南镇综合行政执法辅助人员及便民服务人员招聘调整考试参考试题及答案详解
- 2026广东惠州市博罗县自然资源局补充招聘编外人员6人(第二次)笔试备考题库及答案详解
- (青桐鸣联考)河南新乡等地2025-2026学年下学期高二期末考试政治+答案
- 创意造型设计课件
- 《生态环境法典》企业负责人合规培训
- 2026中国再生资源行业人才需求特征与培养机制研究报告
- 人教版八年级上册道德与法治知识点总结
- 2025版糖尿病视网膜病变诊断与护理指南
- GB/T 8325-2026塑料聚合物分散体和橡胶胶乳pH值的测定
- GB/T 4340.2-2025金属材料维氏硬度试验第2部分:硬度计的检验与校准
评论
0/150
提交评论