簡體   English   中英

如何在JVM被殺死之前使線程永遠存活?

[英]How to keep a thread alive forever until JVM is killed?

我有一個要一直運行直到JVM停止的線程。 最好的方法是什么?

public void run() {
    String event = sc.nextLine();
    try {
          queue.put(event); // thread will block here
    } catch (InterruptedException e) {
          e.printStackTrace();
    }
}

僅添加無限循環即可解決問題

public void run() {
   while(true){
        String event = sc.nextLine();
        try {
          queue.put(event); // thread will block here
        } catch (InterruptedException e) {
           e.printStackTrace();
        }
    }
}
while (true) { runBody(); }

如有必要,添加異常處理。

暫無
暫無

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

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