Java-如何检查';ThreadLocal';Eclipse中的变量?
我的web应用程序中填充了两个Java-如何检查';ThreadLocal';Eclipse中的变量?,java,eclipse,debugging,thread-local,Java,Eclipse,Debugging,Thread Local,我的web应用程序中填充了两个ThreadLocals。而且,在远程调试webapp时,我想在Eclipse中查看这些ThreadLocal变量的值(就像Eclipse在调试透视图的变量选项卡中显示其他变量的方式一样) 知道在Eclipse中调试期间如何查看ThreadLocal变量的值吗? 谢谢 在代码中,必须将值放入局部变量中,您可以看到。您应该能够在使用ThreadLocal的位置设置断点 问题是调试器的连接与您感兴趣的线程位于不同的线程上。Eclipse可能对此有一个解决方案,但我不知道
ThreadLocal
s。而且,在远程调试webapp时,我想在Eclipse
中查看这些ThreadLocal
变量的值(就像Eclipse
在调试透视图的变量选项卡中显示其他变量的方式一样)
知道在Eclipse
中调试期间如何查看ThreadLocal
变量的值吗?
谢谢 在代码中,必须将值放入局部变量中,您可以看到。您应该能够在使用ThreadLocal的位置设置断点
问题是调试器的连接与您感兴趣的线程位于不同的线程上。Eclipse可能对此有一个解决方案,但我不知道它是什么。当您遇到任何断点时,只需使用view
Expressions
来显示Thread.currentThread()
的值,就可以检查每个ThreadLocal值
如果您想了解更多详细信息,即想查看所有线程的threadlocal变量,以下代码可能会有所帮助:
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();
}
公共静态字符串printThreadLocal(){
StringBuilder sb=新的StringBuilder();
试一试{
线程[]tarray=新线程[Thread.activeCount()];
线程枚举(tarray);
对于(int i=0;i
您可以将
MyClass.printThreadLocal()
添加到表达式中
@peakit我不懂。甚至我也需要调试线程局部变量。我还尝试使用VisualVM查看这些线程的利用率。我不知道如何在“探查器”选项卡中检查它们。你能解释一下吗?@NagarajanShanmuganathan只有在获取()值并将其放入局部变量时,才能看到ThreadLocal的内容。