46 lines
1.3 KiB
Java
46 lines
1.3 KiB
Java
// com/mindviewinc/util/TaskManager.java
|
|
// Managing and executing a queue of tasks.
|
|
package com.mindviewinc.util;
|
|
import java.util.concurrent.*;
|
|
import java.util.*;
|
|
|
|
public class TaskManager<R,C extends Callable<R>>
|
|
extends ArrayList<TaskItem<R,C>> {
|
|
private ExecutorService exec =
|
|
Executors.newSingleThreadExecutor();
|
|
public void add(C task) {
|
|
add(new TaskItem<>(exec.submit(task),task));
|
|
}
|
|
public List<R> getResults() {
|
|
Iterator<TaskItem<R,C>> items = iterator();
|
|
List<R> results = new ArrayList<>();
|
|
while(items.hasNext()) {
|
|
TaskItem<R,C> item = items.next();
|
|
if(item.future.isDone()) {
|
|
try {
|
|
results.add(item.future.get());
|
|
} catch(InterruptedException |
|
|
ExecutionException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
items.remove();
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
public List<String> purge() {
|
|
Iterator<TaskItem<R,C>> items = iterator();
|
|
List<String> results = new ArrayList<>();
|
|
while(items.hasNext()) {
|
|
TaskItem<R,C> item = items.next();
|
|
// Leave completed tasks for results reporting:
|
|
if(!item.future.isDone()) {
|
|
results.add("Cancelling " + item.task);
|
|
item.future.cancel(true); // Can interrupt
|
|
items.remove();
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
}
|