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

标签 java multithreading debugging intellij-idea

我在 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

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

最佳答案

文档读起来很困惑,但是 this is the relevant block.它归结为将属性设置为在线程上挂起,而不是整个应用程序。这将导致您在每个单独的线程而不是任意的、不确定的线程上打断点。

Suspend box checked with Thread radio button selected.

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

关于java - 如何在 IntelliJ 中调试多线程应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784413/

相关文章:

debugging - 自动化调试工具

c# - 如何找出记录数最大的数组编号?

java - float Action 按钮点击时会跳转

java - 如何从 Guava 集合中更改 ImmutableSet?

sql-server - 如何在存储过程中设置锁?

c# - coreclr 中的 Thread.Yield()

c++ - 在 tbb 中回收 parent 作为 child

debugging - 看似由非复对数产生的复数

java - 从具有相似属性的 List<Object> 创建 List<Set>

java - 如何在另一个 block 中使用属于一个 block 的最终变量?