我是靠谱客的博主 无奈季节,这篇文章主要介绍java长时间,保持Java程序永远运行或持续很长时间的最佳方法是什么?,现在分享给大家,希望可以做个参考。

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程序永远运行或持续很长时间内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部