我在 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](https://i.stack.imgur.com/slO1s.png)
据我所知,所有线程都应该在断点处停止。
该文档读起来令人困惑,但是这是相关的块。它的本质是将属性设置为在线程上挂起,而不是整个应用程序。这将导致您在每个单独的线程上而不是任意的、不确定的线程上命中断点。
![Suspend box checked with Thread radio button selected.](https://i.stack.imgur.com/w7p2Q.png)
- Suspend Policy: All
- Suspend Policy: Thread
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)