ThreadLocal
我的网络应用程序中填充了几个s。而且,在远程调试 webapp 时,我想查看这些ThreadLocal
变量的值Eclipse
(就像Eclipse
在 Debug 透视图中的变量选项卡中显示其他变量的方式一样)。
知道如何ThreadLocal
在调试期间查看变量的值Eclipse
吗?
谢谢!
ThreadLocal
我的网络应用程序中填充了几个s。而且,在远程调试 webapp 时,我想查看这些ThreadLocal
变量的值Eclipse
(就像Eclipse
在 Debug 透视图中的变量选项卡中显示其他变量的方式一样)。
知道如何ThreadLocal
在调试期间查看变量的值Eclipse
吗?
谢谢!
在您的代码中,您必须将值放入您可以看到的局部变量中。您应该能够在使用 ThreadLocal 的位置下断点。
问题是调试器的连接与您感兴趣的连接在不同的线程上。Eclipse 可能对此有解决方案,但我不知道它是什么。
如果您想了解更多详细信息,即想查看所有线程的线程局部变量,则以下代码可能会有所帮助:
public static String printThreadLocal() {
StringBuilder sb = new StringBuilder();
try {
Thread[] tarray = new Thread[Thread.activeCount()];
Thread.enumerate(tarray);
for (int i = 0; i < tarray.length; i++) {
Field threadLocalField = Thread.class.getDeclaredField("threadLocals");
threadLocalField.setAccessible(true);
Object o1 = threadLocalField.get(tarray[i]); //Thread.currentThread());
Field tableField = o1.getClass().getDeclaredField("table");
tableField.setAccessible(true);
Object[] o2 = (Object[]) tableField.get(o1);
for (Object temp : o2) {
if (temp != null) {
Field valueField = temp.getClass().getDeclaredField("value");
valueField.setAccessible(true);
Object o3 = valueField.get(temp);
sb.append(o3.toString() + "\n");
}
}
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
return sb.toString();
}
您可以添加MyClass.printThreadLocal()
到Expressions
.