To answer this question, I wrote a small unit test:
import static org.assertj.core.api.BDDAssertions.then;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
class ScheduledExecutorServiceLT {
@Test
void test() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
final ScheduledFuture<?> scheduledFuture = scheduledExecutor.scheduleWithFixedDelay(() -> {
if (counter.incrementAndGet() == 3) {
throw new RuntimeException();
}
}, 0L, 100L, TimeUnit.MILLISECONDS);
then(scheduledFuture.isCancelled()).isFalse();
then(scheduledFuture.isDone()).isFalse();
Thread.sleep(500L);
then(counter.get()).isEqualTo(3);
then(scheduledFuture.isCancelled()).isFalse();
then(scheduledFuture.isDone()).isTrue();
}
}
Conclusions:
- the
Runnable
will not be executed again, after the exception occured
- In contrast to my expectations, cancelled stays
false
, but
- done becomes
true
, if an exception was raised
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…