I have java app which supports multiple workflows. Workflow is chosen using arguments passed to it from command line. In one of the workflow app needs to run for infinite time. I am achieving the same using following code
switch (args[0]) {
case "-runForever":
// Some Computation
Thread.sleep(Long.MAX_VALUE);
break;
case "otherCase:
//dosomething
break;
}
Is it a good way of achieving the required functionality?
解决方案
You could use an infinite loop:
while(true){}
However, that would eat up the CPU for no reason. Instead, you could just call the wait() method:
synchronized{
wait();
}
Then to resume, you'd call notify() from another thread.
More info here.
You could also just start another non-daemon Thread.
最后
以上就是无奈季节最近收集整理的关于java长时间,保持Java程序永远运行或持续很长时间的最佳方法是什么?的全部内容,更多相关java长时间,保持Java程序永远运行或持续很长时间内容请搜索靠谱客的其他文章。
发表评论 取消回复