我在 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
仍会执行(有时会执行多次)。截图如下:
据我所知,所有线程都应该在断点处停止。
最佳答案
文档读起来很困惑,但是 this is the relevant block.它归结为将属性设置为在线程上挂起,而不是整个应用程序。这将导致您在每个单独的线程而不是任意的、不确定的线程上打断点。
- 暂停政策:全部
- 当一个断点被命中时,所有线程都被挂起。
- 暂停政策:线程
- 当断点被命中时,断点所在的线程被挂起。
关于java - 如何在 IntelliJ 中调试多线程应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784413/