簡體   English   中英

如何為 Runnable 類添加斷點

[英]How to add breakpoints to Runnable classes

我的while(true)只運行一次,所以我試圖添加斷點以查看發生了什么,但它們似乎從未在我的run()到達。 我正在使用 IntelliJ。 在調試器中有一個“線程”選項卡。 我是否需要在該選項卡中執行某些操作,例如選擇正確的線程才能到達斷點? 我還看到線程名稱,想知道如何在此列表中找到正確的線程。

public class MyClass extends ServerWorkflowProcess<OtherClass> {

    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    ...

    @Override
    public void bootup() {
        logger.info("Booting up: " + this);

        BackgroundProcess backgroundImpositioner = new BackgroundProcess(this.getCollection());
        executorService.submit(backgroundImpositioner);
    }

    @Override
    public void shutdown() {
        executorService.shutdown();
    }
}

后台進程

public class BackgroundProcess implements Runnable {

    protected volatile Logger logger = Logger.getLogger(BackgroundImpositioner.class.getName());

    Collection<ImpositionWorkstation> impositionWorkstations;

    public BackgroundImpositioner(Collection<ImpositionWorkstation> impositionWorkstation) {
        this.impositionWorkstations = impositionWorkstation;
    }

    public void run() {
        while(true) {
            logger.info("looping");
            for (ImpositionWorkstation workstation : impositionWorkstations) {
                if (workstation.canAcceptWork()) {

                    //go do work in another thread so we're not blocking this
                    workstation.getWorkFromQueue();
                    try {
                        workstation.doWork();
                    } catch (ImpositionException e) {
                        logger.severe(e.getMessage());
                    }
                }
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                logger.severe("Background impositioner was interrupted");
            }
        }
    }
}

旁注:控制台顯示“循環”,所以我知道它被執行一次。 斷點永遠不會被擊中,也不會執行多次。

我曾經遇到過我無法讓 Intellij Idea 在斷點處停止的情況。 基本上問題在於,一旦一個線程在斷點處停止,其他線程就不會停止。

斷點屬性對話框中有一個設置可以防止這種情況發生。

右鍵單擊斷點並選擇“查看斷點”。

在對話框中選擇一個斷點。

您會注意到掛起復選框的右側有 2 個單選按鈕: AllThread 全選 另外,您可以將其設為默認值(右側的“設為默認值”按鈕)。 默認值將用於您添加的任何新斷點。 舊的需要手動更改。

編輯Intellij 幫助站點上的附加信息: 斷點選項

斷點對話框

不要讓異常悄悄溜走。 使用submit方法返回的Future

Future<?> f=executorService.submit(backgroundImpositioner);
try {
  f.get();
} catch(Exception ex) {
  ex.printStackTrace();
}

然后你知道更多。

上面的代碼只是為了找到你的實際問題。 對於生產環境,您不會等待完成,而是在異常發生時記錄異常,例如:

executorService.execute(new FutureTask<Object>(backgroundImpositioner, null)
{
  @Override
  protected void done() {
    if(!isCancelled()) try {
      get();
    } catch(InterruptedException ex) {
      throw new AssertionError("on completed task", ex);
    } catch(ExecutionException ex) {
      logger.log(Level.SEVERE, "in background task", ex.getCause());
    }
  }
});

出於某種原因,我無法在while(true)行中設置斷點,但可以在run()其他位置刪除斷點。

如果 run 方法中沒有拋出異常,我只能假設其中一個調用永遠不會返回。

你能在每次調用后放置輸出語句來看看你能走多遠嗎? 我猜workstation.canAcceptWork()workstation.doWork()是罪魁禍首。

我遇到了一個類似的問題,即 IntelliJ 從未在 Runnable 類的 run() 方法中命中我的斷點。 我發現斷點的唯一位置是在public void run() {

暫無
暫無

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

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