Java 如何确认ScheduledExecutorService设置正确(并且将来仍在运行)
我有一个Java方法,它使用ScheduledExecutorService来安排任务定期运行 我希望包含代码来检查: 已成功计划任务,即返回true 现在和将来都会尝试运行部分,如果运行部分不工作,则调用方法:doOtherStuff 我正在考虑使用try/catch来实现这一点。我的思路对吗 我的代码是这样的:Java 如何确认ScheduledExecutorService设置正确(并且将来仍在运行),java,scheduling,Java,Scheduling,我有一个Java方法,它使用ScheduledExecutorService来安排任务定期运行 我希望包含代码来检查: 已成功计划任务,即返回true 现在和将来都会尝试运行部分,如果运行部分不工作,则调用方法:doOtherStuff 我正在考虑使用try/catch来实现这一点。我的思路对吗 我的代码是这样的: private void scheduleCheck() { ScheduledExecutorService executor = Executors.newSingleT
private void scheduleCheck()
{
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable()
{
// Task to be scheduled
public void run()
{
periodicCheck();
}
}, 1, 10, TimeUnit.SECONDS);
}
1。我是否应该在调度任务的代码周围添加一个try/catch,如果“try”部分一直运行到最后,则将一个布尔变量设置为true
两个人。我是否可以再次在run部分中使用try/catch,如果运行失败,则在catch位中调用一个方法
我在想这些事情。有人能看到我遗漏了什么,或者我的逻辑中有什么明显的缺陷吗
private boolean scheduleCheck()
{
boolean isScheduled = false;
try
{
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable()
{
// Task to be scheduled
public void run()
{
try
{
periodicCheck();
}
catch(java.lang.exception e)
{
doOtherStuff();
}
}
}, 1, 10, TimeUnit.SECONDS);
isScheduled = true;
}
catch(java.lang.exception e)
{
}
return isScheduled;
}