继续踩着带断点的线程run()

时间:2012-05-30 13:56:00

标签: java multithreading debugging

在尝试调试多线程程序时,我遇到了一个新问题。下面,我列出了一个测试程序,展示了我的问题。本质上,当我在调用线程的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());
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

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

我怀疑如果你在run()中没有断点,那么单步执行start()就行了。它是第二个断点,它阻止了另一个线程并阻止它运行sleep()或返回join()

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

enter image description here

答案 1 :(得分:2)

如果使用eclipse进行调试,则应在调试窗口中看到线程列表。从那里选择另一个线程的堆栈跟踪,你也应该能够调试run方法。