Why does Java's scheduleWithFixedDelay work with a Runnable but not a FutureTask wrapping a runnable?
This can be shown pretty easily with two different code samples:
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("beep");
return 1;
}
}), 1, 5, TimeUnit.SECONDS);
produces:
beep
But the application does not exit, it simply appears to wait.
but:
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("beep ");
}
}, 1, 5, TimeUnit.SECONDS);
produces:
beep beep beep beep beep
at 5 second intervals.
It seems like there is some kind of lock happening here that I cannot determine.