예약 된 작업을 일시 중지했다가 다시 시작해야합니다.예약 된 작업 일시 중지 및 다시 시작
작업을 취소하는 많은 코드 예제가 있지만 작업을 다시 시작하거나 다시 시작해야합니다. 이 작업을 수행하는 제안이 표시됩니다.
public void init(){
scheduledTaskExecutor.getScheduledThreadPoolExecutor().setRemoveOnCancelPolicy(true);
}
public void schedule(String id, Runnable task){
ScheduledFuture<?> scheduledFuture = scheduledTaskExecutor.scheduleAtFixedRate(task);
runningTaskRegistry.put(id, task);
}
public void suspend(String id){
ScheduledFuture<?> scheduledFuture = runningTaskRegistry.get(serviceName);
if(scheduledFuture != null){
scheduledFuture.cancel(false);
try{
scheduledFuture.get();
} catch (InterruptedException | ExecutionException | CancellationException e){
// I do not want the currently running task finishing after this point
}
}
}
... 그런 다음 다시 시작하지 않고 다시 예약해야합니다.
이렇게하는 것이 가장 좋은 방법입니까?
일반적으로 작업을 다시 예약하는 것이 좋습니다. 마음에 드는 해킹은 [단일 스레드 실행 프로그램] (http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newSingleThreadScheduledExecutor--)을 사용하는 것입니다. 일시 중지 조건이 거짓이 될 때까지 대기하는 태스크를 전달합니다. – VGR
단일 스레드 실행 프로그램은 여기에 옵션이 없습니다. 실행하기 위해 수십 개의 장기 실행 프로세스가 있으며 그 중 일부는 꽤 오래 실행됩니다. – BTakacs