2012-03-15 3 views
0

: 한 번만 발생 작업에 대해 이야기 할 때(취소) 및 정기 예약 된 작업 안드로이드 데브 참조에서

public abstract boolean cancel (boolean mayInterruptIfRunning) 

If the task has already started, then the mayInterruptIfRunning parameter 
determines whether the thread executing this task should be interrupted in 
an attempt to stop the task. 

이것은 나를 위해 분명하다. 하지만, 정기적 인 작업이있을 때, 현재의 "작업"을 끝내고 싶지만 새로운 작업을 시작하지 않으려 고합니다.

나도 똑바로 바로 사용할 수없는 것이 맞습니까? 인수를 true로 설정하면 현재 작업이 끝나기 전에 중지되고, false로 변경하면 현재 실행중인 작업이있는 것으로 간주됩니다. 이게 옳은 거니?

내가 원하는 것을 어떻게 구현합니까? 어떻게 든 실행 중인지 찾기 위해 작업을 폴링해야하고, 그렇지 않을 때 취소할까요?

답변

0

환경에 대한 확신이 없지만 정기적 인 작업 일정이 잡힌 java.util.concurrent의 일부 실행 프로그램을 사용하고 있다는 것을 이해하는 한 자세한 내용은 확실하지 않습니다.

기본 매개 변수 cancel()은 실행중인 스레드를 강제 종료할지 여부를 결정합니다. 이 값을 false로 설정하면 프로그래머는 및 Runnable에서 파생 된 내용을 말하면서 isCanceled()을 검사하여 실행중인 스레드를 인터럽트 할시기를 결정할 수 있습니다.

따라서 cancel(false) 메서드를 SheduledFuture으로 호출하면 Executor이 반환됩니다. 이렇게하면 실행중인 작업이 중단되지 않고 다시 실행되지 않습니다. 호출 한 후 Future.get()은 예외를 throw하여 실행 프로그램이 다음 예약 된 시간에 실행하지 못하게합니다.

0

사용 ScheduledThreadPoolExecutor.shutdown() :

... ... 
// At some point in the future if you want reject next periodic task submit 
// after waiting for current periodic task finish. 
scheduledThreadPoolExecutor.shutdown(); // <-- this will reject next periodic task submit. 
scheduledThreadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS); // <-- this will wait current periodic task finish. 
... ... 

awaitTermination()() 종료 후 호출 할 필요가 명확 안드로이드 API에서 언급되지 않지만, 당신은 official Java API에서 자세한 문서를 찾을 수 있습니다.