如何在JVM被杀之前永远保持线程活着?

时间:2016-03-04 12:25:30

标签: java multithreading

我有一个线程,我想一直运行直到JVM停止。最好的方法是什么?

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

2 个答案:

答案 0 :(得分:3)

只需添加一个无限循环即可。

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

答案 1 :(得分:1)

while (true) { runBody(); }

如有必要,添加例外处理。