我是java编程的stater ...我正在编写一个调度程序。我正在从属性文件中读取日期和时间值。如果属性文件中有任何更改,那么我必须重新安排调度程序。因此,为此我正在编写一个无限 for 循环,如下所示:
for(;;){
result = loadData.checkChanges();
if(result == true){
//If changes found in the properties file then reSchedule tasks
reSchedule();
}
}
如果循环运行到应用程序在“应用程序服务器”中的生命周期(一年或两年),它会遇到任何性能问题或JVM问题还是有其他问题?
最佳答案
除非您的 checkChanges()
方法 hibernate 或阻塞,否则当前编写的代码将通过调用 loadData.checkChanges();
来破坏 CPU,其速度与处理器的速度一样快有能力的。理想情况下,您应该执行诸如调用 loadData.wait() 之类的操作,然后在您希望运行检查时让另一个线程调用 loadData.notify() 。一个更简单且几乎同样好的解决方案是定期 hibernate 线程,例如:
for(;;){
result = loadData.checkChanges();
if(result == true){
//If changes found in the properties file then reSchedule tasks
reSchedule();
}
try {
Thread.sleep(100);
}
catch (InterruptedException ignored) {}
}
此外,请不要使用for(;;)
。相反,尝试使用 while(true)
,这样就清楚多了。或者更好的是,尝试 while(!stop)
,其中 stop
是一个 boolean 变量,当应用程序上下文被破坏(服务器关闭、Web 应用程序取消部署等)时,该变量会设置为 true。 )。
关于java - 无限for循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6406206/