因此,使用此链接作为参考,是否有人可以建议更优雅的解决方案来取消定期的ScheduledExecutorService任务?
这是我目前正在做的一个例子:
// do stuff
// Schedule periodic task
currentTask = exec.scheduleAtFixedRate(
new RequestProgressRunnable(),
0,
5000,
TimeUnit.MILLISECONDS);
// Runnable
private class RequestProgressRunnable implements Runnable
{
// Field members
private Integer progressValue = 0;
@Override
public void run()
{
// do stuff
// Check progress value
if (progressValue == 100)
{
// Cancel task
getFuture().cancel(true);
}
else
{
// Increment progress value
progressValue += 10;
}
}
}
/**
* Gets the future object of the scheduled task
* …Run Code Online (Sandbox Code Playgroud)