질문은 : 방법 무기한 (waybe 자원을 기다리고, 또는 다른 뭔가를) 차단되지 않습니다 있는지 확인하는 feasibile 방법이있다?
일정한 일정 간격으로 일정을 계획 할 수없는 경우 고정 지연을 사용하지 말고 지연 + 최종 실행 완료라는 두 가지 조건을 사용해야합니다.
두 조건이 충족되는지 확인하는 작업을 예약 할 수 있으며,이 경우 중요한 처리를 실행합니다. 그렇지 않으면 다음 일정을 기다립니다.
이렇게하면 차단해서는 안됩니다. 작업이 고정 지연을 초과하면 얼마 동안 기다릴 수 있습니다. 고정 지연이 자주 초과되어 문제가되는 경우 고정 지연을 사용하지 않아야하므로 일반적이지 않으므로 민감하게 증가시켜야합니다. 여기
예
은 (편집없이 서면 어떤 실수하는 경우 죄송합니다.) :
이
private boolean isLastImportDataTaskFinished;
@Scheduled(fixedDelay = 300000)
public void importDataTaskManager(){
if (isLastImportDataTaskFinished()){
new Thread(new ImportantDataProcessing())).start();
}
else{
// log the problem if you want
}
}
private isLastImportDataTaskFinished(){
// to retrieve this information, you can do as you want : use a variable
// in this class or a data in database,file...
// here a simple implementation
return isLastImportDataTaskFinished;
}
의 Runnable 클래스 :
public class ImportantDataProcessing implements Runnable{
public void run(){
importData(); //db calls, file manipulations, etc..
}
}
이
코멘트 :
하지만 실행하는 경우 스레드로 어떻게하면 그것을 초과 찾을 수있는 그것을 죽일 수 이후 아무런 언급이 없으므로 시간 제한이 적용됩니다 ( 두 번째 작업을 사용하여 고정 된 상태를 파악할 수 있음).
ExecutorService를 사용할 수 있습니다 (여기에 대한 질문이 있습니다 : How to timeout a thread). 다음은 아주 간단한 예를
: 흥미로운 정보가 ImportantDataProcessing
처리에 의해 반환 될 수 있습니다
ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(new ImportantDataProcessing());
try {
future.get(100, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ExecutionException e) {
e.printStackTrace();
}
catch (TimeoutException e) {
// the timeout to handle but other exceptions should be handled :)
e.printStackTrace();
}
executor.shutdown();
경우에, 당신은 미래를 입력하는 대신 실행 가능한 인스턴스의 작업을 할 수 있습니다.
교착 상태를 찾는 것이 더 나은 방법이 될 것입니다. 그래서 우리가 수정을위한 아이디어를 제공 할 수 있을지는 의문입니다. –