我有一个线程,我想一直运行它直到 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();
}
}
}
关于java - 如何在 JVM 被杀死之前让线程永远存活?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796036/