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

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

问题描述

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

  import java.util.concurrent.atomic.AtomicInteger; 


public class Main {

private static final int NUM_CLIENTS = 1000;

静态类TestRunnable实现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); (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 仍然执行(有时几个次)。这是一个截图:





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

解决方案

文件令人困惑,但这是相关的部分。直到将属性设置为挂起线程,而不是整个应用程序。这将导致您打破每个线程的断点,而不是任意的不确定的线程。






  • 暂停政策:全部


    • 当断点被击中时,所有线程都被暂停。


  • 暂停策略:线程


    • 当断点被击中时,断点被击中的线程被暂停。

    / li>

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

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.

  • 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.

这篇关于如何在IntelliJ中调试多线程应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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