[英]How to keep a thread alive forever until JVM is killed?
I have a thread which i wanted to run always until the JVM is stopped. 我有一个要一直运行直到JVM停止的线程。 What is the best way to do that ?
最好的方法是什么?
public void run() {
String event = sc.nextLine();
try {
queue.put(event); // thread will block here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Just adding an infinite loop should do the trick 仅添加无限循环即可解决问题
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.