threadLoccal 里面有什么?
2016-11-09 21:04
183 查看
老外的:
偶自己的:
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;
//转换为数组,迭代entry
for(int i = 0 ; i < tableArray.length ; i++) {
//System.out.println(tableArray[i]);
if(null != tableArray[i] ) {
//打印value
Object value = getField(tableArray[i],"value");
System.out.println("====================");
printFiledNames(value);
System.out.println("====================");
System.out.println(value);
if(value != null){
//获取refer的链接对象,可以不用反射了,直接强转为Reference
Method 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());
}
}
}
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;
//转换为数组,迭代entry
for(int i = 0 ; i < tableArray.length ; i++) {
//System.out.println(tableArray[i]);
if(null != tableArray[i] ) {
//打印value
Object value = getField(tableArray[i],"value");
System.out.println("====================");
printFiledNames(value);
System.out.println("====================");
System.out.println(value);
if(value != null){
//获取refer的链接对象,可以不用反射了,直接强转为Reference
Method 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());
}
}
}
相关文章推荐
- 外贸里面 LC TT DP DA BG 是什么
- precreatewindow里面做了什么?
- web上下文是什么?里面有什么对象?request,session,application它们和servlet有什么关系!
- 【2014】电脑里面的各个组件都分别怕什么
- Java基本概念(2)J2EE里面的2是什么意思
- VR应用里面的Photogrammetry技术是什么
- android里面layer-list中的inset和clip到底有什么作用
- 操作系统在hello world里面做了什么
- C#.NET里面抽象类和接口有什么区别?
- C#里面Console.Write与Console.WriteLine有什么区别????
- uc浏览器设置里面的的浏览器ua是什么意思
- Fiddler响应post的请求 request body里面填写什么?
- 在逮捕异常的时候 可以获取e.MESSAGE里面的信息 然后判断是什么异常
- FLEX学习笔记:AS里面的Object有什么特别之处?
- null作为方法的参数,并在方法里面赋值后的结果是什么?
- .NET里面 abstract class和Interface有什么区别以及用法的展现?
- 包装很好,里面是什么?
- js里面的单引号和双引号有什么区别??
- php里面bcadd是什么意思
- 程序集引用里面的“Culture=neutral”是什么意思?