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

时间:2021-04-09 21:01:28

I'm having a strange issue with multiple threads and breakpoints in IntelliJ IDEA 14.0.2. Code after the breakpoint is executed before it stops on it.

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

When I put a breakpoint (Suspend All) at line 12, synchronized (this.lock), System.out.println still executes (sometimes several times). Here's a screenshot:

当我在第12行放置一个断点(Suspend All)时,同步(this.lock),System.out.println仍然执行(有时几次)。这是一个截图:

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

As far as I know, all threads should stop at the breakpoint.

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

1 个解决方案

#1


89  

The documentation reads confusingly, but this is the relevant block. What it distills down to is setting the property to suspend on threads, and not the entire application instead. This will cause you to hit the break point on each individual thread instead of an arbitrary, indeterminate thread.

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

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

  • Suspend Policy: All
    • When a breakpoint is hit, all threads are suspended.
    • 当命中断点时,所有线程都被挂起。

  • 挂起策略:全部当命中断点时,所有线程都被挂起。

  • Suspend Policy: Thread
    • When the breakpoint is hit, the thread where the breakpoint is hit is suspended.
    • 当命中断点时,挂起断点的线程被挂起。

  • 挂起策略:线程当命中断点时,挂起断点的线程被挂起。

#1


89  

The documentation reads confusingly, but this is the relevant block. What it distills down to is setting the property to suspend on threads, and not the entire application instead. This will cause you to hit the break point on each individual thread instead of an arbitrary, indeterminate thread.

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

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

  • Suspend Policy: All
    • When a breakpoint is hit, all threads are suspended.
    • 当命中断点时,所有线程都被挂起。

  • 挂起策略:全部当命中断点时,所有线程都被挂起。

  • Suspend Policy: Thread
    • When the breakpoint is hit, the thread where the breakpoint is hit is suspended.
    • 当命中断点时,挂起断点的线程被挂起。

  • 挂起策略:线程当命中断点时,挂起断点的线程被挂起。