如何在 IntelliJ 中调试多线程应用程序? [英] How to debug a multi-threaded app in IntelliJ?

查看:35
本文介绍了如何在 IntelliJ 中调试多线程应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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 行设置断点 (Suspend All) 时,synchronized (this.lock)System.out.println 仍然会执行(有时会执行几次).截图如下:

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:

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

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

推荐答案

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

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 中调试多线程应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆