I'm just starting to look into Futures and the ScheduledExecutorService in Java, and I'm wondering why my Callable isn't running on the schedule I've indicated. In this sample code, the callable runs once, but the app never completes, nor does the task run again, which is what I expected to happen (I'm sure the problem is with my expectation).
Runnables work fine; Callables seem to block forever, but I'm not sure why.... What am I missing?
Thanks!
public class ExecutorExample {
/**
* @param args
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
FutureTask<ArrayList<String>> ft1 = new FutureTask<ArrayList<String>>(new Callable<ArrayList<String>>(){
@Override
public ArrayList<String> call() {
ArrayList<String> stuff = new ArrayList<String>();
for(int i = 0;i<10;i++){
String thing ="Adding " + i + " to result";
stuff.add(thing);
System.out.println(thing);
}
return stuff;
}});
scheduler.scheduleAtFixedRate(ft1, 0, 1, TimeUnit.SECONDS);
System.out.println(ft1.get());
System.out.println(ft1.isDone());
}
}