package jalview.workers;
import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import jalview.api.AlignCalcListener;
-import jalview.api.AlignCalcManagerI;
import jalview.api.AlignCalcManagerI2;
import jalview.api.AlignCalcWorkerI;
+import jalview.bin.Cache;
import jalview.datamodel.AlignmentAnnotation;
import static java.util.Collections.synchronizedList;
import java.util.ArrayList;
import java.util.HashSet;
+import static java.lang.String.format;
+
public class AlignCalcManager2 implements AlignCalcManagerI2
{
class AlignCalcTask extends FutureTask<Void>
{
super(new Callable<Void>() {
public Void call() throws Exception {
+ Cache.log.debug(format("Worker %s started%n", worker.getClass().getName()));
notifyStarted(worker);
worker.run();
return null;
{
get();
success = true;
- } catch (ExecutionException e)
+ }
+ catch (ExecutionException e)
{
exception = e.getCause();
if (exception instanceof OutOfMemoryError) {
tasks.remove(this);
}
if (success)
+ {
+ Cache.log.debug(format("Worker %s finished%n", getWorker().getClass().getName()));
notifyCompleted(worker);
- else
+ }
+ else if (exception != null){
+ Cache.log.warn(format("Worker %s failed%n", getWorker().getClass().getName()));
+ exception.printStackTrace();
notifyExceptional(worker, exception);
+ }
}
}
@Override
public void registerWorker(AlignCalcWorkerI worker)
{
+ Objects.requireNonNull(worker);
synchronized (registered)
{
if (!registered.contains(worker))
{
synchronized (registered)
{
- for (var it = registered.iterator(); it.hasNext();)
+ for (var worker : registered)
{
- var worker = it.next();
if (worker.getClass().equals(cls))
{
- it.remove();
- disabled.remove(worker);
+ removeWorker(worker);
}
}
}
{
synchronized (registered)
{
- for (var it = registered.iterator(); it.hasNext();)
+ for (var worker : registered)
{
- var worker = it.next();
if (worker.involves(annot) && worker.isDeletable())
{
- it.remove();
- disabled.remove(worker);
+ removeWorker(worker);
}
}
}
@Override
public void disableWorker(AlignCalcWorkerI worker)
{
- assert registered.contains(worker);
disabled.add(worker);
}
@Override
public void startWorker(AlignCalcWorkerI worker)
{
- assert registered.contains(worker);
- synchronized (tasks) {
- for (var task : tasks)
- {
- if (task.getWorker().equals(worker))
- task.cancel(true);
- }
- }
+ Objects.requireNonNull(worker);
AlignCalcTask newTask = new AlignCalcTask(worker);
- tasks.add(newTask);
+ synchronized (inProgress)
+ {
+ cancelWorker(worker);
+ inProgress.add(worker);
+ tasks.add(newTask);
+ }
notifyQueued(worker);
executor.execute(newTask);
}
{
synchronized (tasks)
{
- for (var task : tasks)
- {
- if (task.getWorker().equals(worker))
- task.cancel(true);
+ Optional<AlignCalcTask> oldTask = tasks.stream()
+ .filter(task -> task.getWorker().equals(worker))
+ .findFirst();
+ if (oldTask.isPresent()) {
+ oldTask.get().cancel(true);
}
}
}