线程上下文ThreadContext.docx_第1页
线程上下文ThreadContext.docx_第2页
线程上下文ThreadContext.docx_第3页
线程上下文ThreadContext.docx_第4页
线程上下文ThreadContext.docx_第5页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论