我有一个线程,我想一直运行直到JVM停止。最好的方法是什么?
public void run() {
String event = sc.nextLine();
try {
queue.put(event); // thread will block here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
答案 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(); }
如有必要,添加例外处理。