老外的:
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());
}
}
}