本文介绍了在java中是否有Thread.sleep的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
while (true) {
try {
Thread.sleep(5 * 60 * 1000);
processData();// data processing job
} catch (InterruptedException e) {
SystemMessageBillPay.getInstance().writeMessage("ERROR: CEB.run() - " + e.getMessage());
} catch (NumberFormatException e) {
SystemMessageBillPay.getInstance().writeMessage("ERROR: CEB.run() - " + e.getMessage());
}
}
到此
代码检查员发出警告
防止此警告的代码应该是什么
What should be the code to prevent this warning
推荐答案
Is there a alternative to Thread.sleep
是的,有办法。 但是,
看起来你想安排一些工作。您可以使用或而不是此。所以后面的部分问题是关于
It looks like you want to schedule some job. You can either use TimerTask or ScheduledExecutorService instead of this. So later part of your question is about the
这将通过安排任务来解决。
and this will solve with scheduling the task.
安排任务。
public class Test extends TimerTask{
public static void main(String[] args) {
Test task = new Test();
Timer timer = new Timer();
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 13);
today.set(Calendar.MINUTE, 47);
today.set(Calendar.SECOND, 0);
timer.schedule(task, today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
}
@Override
public void run() {
System.out.println("Running Scheduled Task...!!!");
}
}
这篇关于在java中是否有Thread.sleep的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!