threadLoccal 里面有什么?
来源:互联网 发布:d3.js api 中文下载 编辑:程序博客网 时间:2024/04/25 12:51
老外的:
package util;import java.lang.ref.Reference;import java.lang.reflect.Field;import java.util.Arrays;public class ThreadLocalUtil { public static void dumphreadLocals() { try { // Get a reference to the thread locals table of the current thread Thread thread = Thread.currentThread(); Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); Object threadLocalTable = threadLocalsField.get(thread); // Get a reference to the array holding the thread local variables inside the // ThreadLocalMap of the current thread @SuppressWarnings("rawtypes")Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); Field tableField = threadLocalMapClass.getDeclaredField("table"); tableField.setAccessible(true); Object[] table = (Object[]) tableField.get(threadLocalTable); @SuppressWarnings("rawtypes")Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry"); Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value"); entryValueField.setAccessible(true); // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object // is a reference to the actual ThreadLocal variable Field referentField = Reference.class.getDeclaredField("referent"); referentField.setAccessible(true); for (Object entry:table) { // Each entry in the table array of ThreadLocalMap is an Entry object // representing the thread local reference and its value if (entry != null) { Object tlcValue=entryValueField.get(entry); //ThreadLocal threadLocal = (ThreadLocal)referentField.get(entry); //System.out.println("thread local value "+tlcValue); printObject(tlcValue); } } } catch(Exception e) { // We will tolerate an exception here and just log it throw new IllegalStateException(e); } } @SuppressWarnings("rawtypes")private static void printObject(Object obj) { System.out.print("find threadlocal var :"); if(obj instanceof Object[]) { System.out.println(Arrays.deepToString((Object[]) obj)); }else if (obj instanceof java.lang.ref.Reference) { java.lang.ref.Reference ref=(Reference) obj; System.out.println(" ref "+ref.getClass().getName()+" ref to "+ref.get()); }else { System.out.println(obj); } }public static void main(String[] args) throws NoSuchMethodException, SecurityException{dumphreadLocals();}}
偶自己的:
import java.lang.reflect.Field;import java.lang.reflect.Method;//import java.lang.StringCoding;public class testThreadLoacal {/** * @param args * @throws SecurityException * @throws NoSuchFieldException */public static void main(String[] args) throws Exception {Thread t = Thread.currentThread();//获取threadLocals对象Object threadLocals = getField(t,"threadLocals");//获取table对象啊Object table = getField(threadLocals, "table");Object[] tableArray = null;if(table.getClass().isArray()) {//数组强转tableArray = (Object[])table;//转换为数组,迭代entryfor(int i = 0 ; i < tableArray.length ; i++) {//System.out.println(tableArray[i]);if(null != tableArray[i] ) {//打印valueObject value = getField(tableArray[i],"value");System.out.println("====================");printFiledNames(value);System.out.println("====================");System.out.println(value);if(value != null){//获取refer的链接对象,可以不用反射了,直接强转为ReferenceMethod getMethod = value.getClass().getMethod("get");Object softReference = getMethod.invoke(value);System.out.println(softReference);for(Method method : softReference.getClass().getDeclaredMethods()){System.out.println(method.getName());}// System.out.println(getField(value,"referent"));;}}}}//Field[] fields = threadLocalsClazz.getDeclaredFields();System.out.println(table);}public static Object getField(Object instance ,String fieldName) throws Exception {Class threadLocalsClazz = instance.getClass();Field tableField = threadLocalsClazz.getDeclaredField(fieldName);tableField.setAccessible(true);Object table = tableField.get(instance);return table;}public static void printFiledNames(Object object) {Field[] fields = object.getClass().getFields();for(int i = 0 ; i < fields.length;i++) {System.out.println(fields[i].getName());}}}
0 0
- threadLoccal 里面有什么?
- BREW里面有什么
- 通知里面到底有什么?
- 目标文件里面到底有什么(1)?
- 目标文件里面到底有什么(2)?
- 正则表达式里面 .* 和 .*? 有什么区别?
- Android SDK里面都有什么
- label里面的for有什么意义
- 目标文件里面到底有什么(1)?
- 目标文件里面到底有什么(2)?
- 网页音乐播放,看里面有没有什么好东西
- database里面inner join和outer join有什么区别
- VC里面TEXT()是什么和_T有什么区别
- MFC里面ON_COMMAND和ON_MESSAGE有什么区别
- C#.NET里面抽象类和接口有什么区别?
- Delphi里面hwnd跟Thandle有什么区别?
- (void)fprintf 里面的(void)有什么作用
- C语言里面的"\r"与"\n"有什么区别?
- 学习资源
- mysql 命令行登录
- java POI读取Excel 2003/2007/2010
- mosquitto 扩展安装php客户端库
- 矩阵的快速取幂法——求类Fibonacci数列(1)
- threadLoccal 里面有什么?
- 2016-11-09 临界知识修订建议
- 上海求职之路第三天
- sublime text3实用教程-安装js智能提示插件
- JavaScript中的函数调用模式
- requestFeature() must be called before adding content错误
- 最大流入门 poj 1273 && hdu 3549
- 混频器与变频器在射频通信中的区别
- [原创]jquery.ztree使用(一)