Java - 如何检查“ThreadLocal”的值Eclipse 中的变量?

发布于 2024-10-04 03:24:02 字数 230 浏览 7 评论 0原文

我的网络应用程序中填充了几个 ThreadLocal。而且,在远程调试 web 应用程序时,我想在 Eclipse 中查看这些 ThreadLocal 变量的值(就像 Eclipse 显示其他变量的方式一样)在调试视角的变量选项卡中)。

知道如何在 Eclipse 中调试期间查看 ThreadLocal 变量的值吗?

谢谢!

I have couple of ThreadLocals populated in my web app. And, while remote debugging the webapp, I want to see the value of these ThreadLocal variables in Eclipse (just like the way Eclipse shows other variables in the variables tab in Debug perspective).

Any idea how can I view the value of ThreadLocal variables during debugging in Eclipse?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲世九天 2024-10-11 03:24:02

当您遇到任何断点时,只需使用视图Expressions来显示Thread.currentThread()的值,您就可以检查每个ThreadLocal值。

输入图像描述这里

When you hit any breakpoint, just use view Expressions to display the value of Thread.currentThread() and you will be able to inspect every ThreadLocal value.

enter image description here

千寻… 2024-10-11 03:24:02

在您的代码中,您必须将这些值放入您可以看到的局部变量中。您应该能够在使用 ThreadLocal 的地方设置断点。

问题是调试器的连接与您感兴趣的线程位于不同的线程上。Eclipse 可以为此提供解决方案,但我不知道它是什么。

In your code you have to place the values into a local variable, which you can see. You should be able to breakpoint where the ThreadLocal is used.

The problem is that the debugger's connection is on a different thread to the one you are interested in. Eclipse could have a solution for this, but I don't know what it is.

高速公鹿 2024-10-11 03:24:02

如果您想要更多详细信息,即想要查看所有线程的线程局部变量,以下代码可能会有所帮助:

    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

If you want more details i.e. want to see the threadlocal variables for all the threads following code might help:

    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();
    }

You can add MyClass.printThreadLocal() to Expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文