【问题标题】:Hang on stepping over thread run() with breakpoints挂断点越过线程 run()
【发布时间】:2012-05-30 13:55:59
【问题描述】:

我在尝试调试多线程程序时遇到了一个新问题。下面,我包含了一个测试程序来演示我的问题。本质上,当我在对线程的 start() 方法以及该线程的 run() 方法的调用上设置断点时,如果我在调试器中“继续”,一切都会好起来的。但是,如果我跳过,我可以跳过对 start()、println() 和 join() 的调用,之后程序等待执行完成,但这永远不会发生。线程似乎没有执行,我在线程的 run() 中的断点永远不会跳闸。我必须退出程序。

现在,如果run() 上没有断点,我继续还是单步都没有关系;线程执行。我在这里错过了什么?

请注意,我正在使用 vanilla eclipse 调试。

public class Test {
    public static void main(String[] args) {
        try {
            Thread myThread = new Test().new TestThread();
            long threadStartTime = System.currentTimeMillis();
            myThread.start(); // **********Breakpoint 1
            System.out.println("Thread started");
            myThread.join();
            long threadExecutionTime = System.currentTimeMillis() - threadStartTime;
            System.out.println("Thread finished execution in " + threadExecutionTime + " milliseconds.");
        } catch(InterruptedException e) {
            System.err.println(e.getMessage());
        }
    }

    private class TestThread extends Thread {
        @Override
        public void run() { // **********Breakpoint 2
            try {
                sleep(5*1000);
            } catch(InterruptedException e) {
                System.err.println(e.getMessage());
            }
        }
    }
}

【问题讨论】:

    标签: java multithreading debugging


    【解决方案1】:

    您确定run() 中的断点没有被触发吗?在 Eclipse 中,您需要单击第二个线程才能看到它已暂停。我怀疑线程已经启动但正在等待你说继续。

    我怀疑如果您在 run() 中没有断点,那么跨过 start() 将可以正常工作。第二个断点阻止了另一个线程并阻止它运行sleep() 或返回join()

    在下图中,您可以看到我跳过了start() 方法并启动了Thread-1。但它已暂停(请参阅黄色暂停条)等待您切换到它并继续。

    【讨论】:

      【解决方案2】:

      如果您使用 Eclipse 进行调试,您应该会在调试窗口中看到线程列表。从那里选择另一个线程的堆栈跟踪,您应该也可以调试运行方法。

      【讨论】:

        猜你喜欢
        • 2011-04-15
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 2020-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        相关资源
        最近更新 更多