如何在 IntelliJ 中调试多线程应用?

我在IntelliJ IDEA 14.0.2中遇到了一个奇怪的问题,有多个线程和断点。断点之后的代码在断点停止之前执行。

import java.util.concurrent.atomic.AtomicInteger;


public class Main {

    private static final int NUM_CLIENTS = 1000;

    static class TestRunnable implements Runnable {
        AtomicInteger lock;
        @Override
        public void run() {
            synchronized (this.lock) {
                int curCounter = this.lock.addAndGet(1);
                System.out.println("Thread: " + Thread.currentThread().getName() + "; Count: " + curCounter);
                if (curCounter >= NUM_CLIENTS) {
                    lock.notifyAll();
                }
            }
        }
    }

    public static void main(final String args[]) {
        final AtomicInteger lock = new AtomicInteger(0);
        for (int i = 0; i < NUM_CLIENTS; i++) {
            TestRunnable tr1 = new TestRunnable();
            tr1.lock = lock;
            new Thread(tr1).start();
        }
        synchronized (lock) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Main woken up");
        }
    }
}

当我在第 12 行放置一个断点(全部挂起)时,仍然执行(有时执行几次)。下面是一个屏幕截图:synchronized (this.lock)System.out.println

enter image description here

据我所知,所有线程都应该在断点处停止。


答案 1

文档读起来令人困惑,但这是相关的块。它提炼为将属性设置为在线程上挂起,而不是在整个应用程序上挂起。这将导致您在每个单独的线程上命中断点,而不是任意的不确定线程。

Suspend box checked with Thread radio button selected.

  • 暂停政策:全部
    • 当命中断点时,所有线程都将挂起。
  • 挂起策略:线程
    • 当命中断点时,命中断点的线程将挂起。

答案 2