簡體   English   中英

如何在IntelliJ中調試多線程應用程序?

[英]How to debug a multi-threaded app in IntelliJ?

我在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仍然執行(有時幾次)。 這是屏幕截圖:

在此處輸入圖片說明

據我所知,所有線程都應在斷點處停止。

文檔閱讀混亂,但這是相關的塊。 歸結為將屬性設置為在線程上掛起,而不是整個應用程序掛起。 這將導致您擊中每個單獨線程的斷點,而不是任意不確定的線程。

選中了“線程”單選按鈕的“掛起”框。

  • 暫停政策:全部
    • 遇到斷點時,所有線程都將掛起。
  • 掛起策略:線程
    • 遇到斷點時,將掛斷該斷點所在的線程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM