X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fjalview%2Fworkers%2FAlignCalcManager.java;h=addb372bd9234a6127ba7dde80803b58163db1e4;hb=ce50fa39f4fb3617b344d77f230caa6486acd0a4;hp=63ba82841e48462fc6791a85bf1683c5d34be318;hpb=8f899b7c0fe8a51c7e10ee522a8d8d1f1719190b;p=jalview.git
diff --git a/src/jalview/workers/AlignCalcManager.java b/src/jalview/workers/AlignCalcManager.java
index 63ba828..addb372 100644
--- a/src/jalview/workers/AlignCalcManager.java
+++ b/src/jalview/workers/AlignCalcManager.java
@@ -1,157 +1,279 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.workers;
+import jalview.api.AlignCalcManagerI;
+import jalview.api.AlignCalcWorkerI;
+import jalview.datamodel.AlignmentAnnotation;
+
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
-
-import jalview.api.AlignCalcManagerI;
-import jalview.api.AlignCalcWorkerI;
-import jalview.datamodel.AlignmentAnnotation;
+import java.util.Set;
public class AlignCalcManager implements AlignCalcManagerI
{
- private volatile List restartable = new ArrayList();
+ /*
+ * list of registered workers
+ */
+ private volatile List restartable;
- private List blackList = new ArrayList();
+ /*
+ * types of worker _not_ to run (for example, because they have
+ * previously thrown errors)
+ */
+ private volatile List> blackList;
- /**
+ /*
* global record of calculations in progress
*/
- private static Hashtable inProgress = new Hashtable();
+ private volatile List inProgress;
- /**
+ /*
* record of calculations pending or in progress in the current context
*/
- private Map> updating = new Hashtable>();
+ private volatile Map, List> updating;
+
+ /*
+ * workers that have run to completion so are candidates for visual-only
+ * update of their results
+ */
+ private HashSet canUpdate;
+
+ /**
+ * Constructor
+ */
+ public AlignCalcManager()
+ {
+ restartable = Collections
+ .synchronizedList(new ArrayList());
+ blackList = Collections
+ .synchronizedList(new ArrayList>());
+ inProgress = Collections
+ .synchronizedList(new ArrayList());
+ updating = Collections
+ .synchronizedMap(new Hashtable, List>());
+ canUpdate = new HashSet();
+ }
@Override
public void notifyStart(AlignCalcWorkerI worker)
{
- List upd = updating.get(worker.getClass());
- if (upd == null)
+ synchronized (updating)
{
- updating.put(worker.getClass(),
- upd = new ArrayList());
+ List upd = updating.get(worker.getClass());
+ if (upd == null)
+ {
+ updating.put(
+ worker.getClass(),
+ upd = Collections
+ .synchronizedList(new ArrayList()));
+ }
+ synchronized (upd)
+ {
+ upd.add(worker);
+ }
}
- upd.add(worker);
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.api.AlignCalcManagerI#isPending(jalview.api.AlignCalcWorkerI)
+ */
@Override
- public synchronized boolean alreadyDoing(AlignCalcWorkerI worker)
+ public boolean isPending(AlignCalcWorkerI workingClass)
{
- return inProgress.containsKey(worker.getClass());
+ List upd;
+ synchronized (updating)
+ {
+ upd = updating.get(workingClass.getClass());
+ if (upd == null)
+ {
+ return false;
+ }
+ synchronized (upd)
+ {
+ if (upd.size() > 1)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
}
@Override
- public synchronized boolean notifyWorking(AlignCalcWorkerI worker)
+ public boolean notifyWorking(AlignCalcWorkerI worker)
{
- // synchronized (inProgress)
+ synchronized (inProgress)
{
- // TODO: decide if we should throw exceptions here if multiple workers
- // start to work
- if (inProgress.get(worker.getClass()) != null)
+ if (inProgress.contains(worker))
{
- if (false)
- {
- System.err
- .println("Warning: Multiple workers are running of type "
- + worker.getClass());
- }
- return false;
+ return false; // worker is already working, so ask caller to wait around
+ }
+ else
+ {
+ inProgress.add(worker);
}
- inProgress.put(worker.getClass(), worker);
}
return true;
}
- private HashSet canUpdate=new HashSet();
@Override
- public synchronized void workerComplete(AlignCalcWorkerI worker)
+ public void workerComplete(AlignCalcWorkerI worker)
{
- inProgress.remove(worker.getClass());
- List upd = updating.get(worker.getClass());
- if (upd != null)
+ synchronized (inProgress)
{
- upd.remove(worker);
- canUpdate.add(worker);
+ // System.err.println("Worker " + worker + " marked as complete.");
+ inProgress.remove(worker);
+ List upd = updating.get(worker.getClass());
+ if (upd != null)
+ {
+ synchronized (upd)
+ {
+ upd.remove(worker);
+ }
+ canUpdate.add(worker);
+ }
}
-
}
@Override
- public void workerCannotRun(AlignCalcWorkerI worker)
+ public void disableWorker(AlignCalcWorkerI worker)
{
- blackList.add(worker.getClass());
+ synchronized (blackList)
+ {
+ blackList.add(worker.getClass());
+ }
}
- public boolean isBlackListed(Class workerType)
+ @Override
+ public boolean isDisabled(AlignCalcWorkerI worker)
{
- return blackList.contains(workerType);
+ synchronized (blackList)
+ {
+ return blackList.contains(worker.getClass());
+ }
}
@Override
public void startWorker(AlignCalcWorkerI worker)
{
- new Thread(worker).start();
+ if (!isDisabled(worker))
+ {
+ Thread tw = new Thread(worker);
+ tw.setName(worker.getClass().toString());
+ tw.start();
+ }
}
@Override
- public synchronized boolean isWorking(AlignCalcWorkerI worker)
+ public boolean isWorking(AlignCalcWorkerI worker)
{
- // System.err.println("isWorking : worker "+(worker!=null ?
- // worker.getClass():"null")+ " "+hashCode());
- return worker != null && inProgress.get(worker.getClass()) == worker;
+ synchronized (inProgress)
+ {// System.err.println("isWorking : worker "+(worker!=null ?
+ // worker.getClass():"null")+ " "+hashCode());
+ return worker != null && inProgress.contains(worker);
+ }
}
@Override
public boolean isWorking()
{
- // System.err.println("isWorking "+hashCode());
- return inProgress.size() > 0;
+ synchronized (inProgress)
+ {
+ // System.err.println("isWorking "+hashCode());
+ return inProgress.size() > 0;
+ }
}
@Override
public void registerWorker(AlignCalcWorkerI worker)
{
- if (!restartable.contains(worker))
+ synchronized (restartable)
{
- restartable.add(worker);
+ if (!restartable.contains(worker))
+ {
+ restartable.add(worker);
+ }
+ startWorker(worker);
}
- startWorker(worker);
}
@Override
public void restartWorkers()
{
- for (AlignCalcWorkerI worker : restartable)
+ synchronized (restartable)
{
- startWorker(worker);
+ for (AlignCalcWorkerI worker : restartable)
+ {
+ startWorker(worker);
+ }
}
}
@Override
public boolean workingInvolvedWith(AlignmentAnnotation alignmentAnnotation)
{
- if (isWorking())
+ synchronized (inProgress)
{
- for (List workers: updating.values())
+ for (AlignCalcWorkerI worker : inProgress)
{
- for (AlignCalcWorkerI worker:workers)
if (worker.involves(alignmentAnnotation))
{
return true;
}
}
}
+ synchronized (updating)
+ {
+ for (List workers : updating.values())
+ {
+ for (AlignCalcWorkerI worker : workers)
+ {
+ if (worker.involves(alignmentAnnotation))
+ {
+ return true;
+ }
+ }
+ }
+ }
return false;
}
@Override
- public void updateAnnotationFor(Class workerClass)
+ public void updateAnnotationFor(
+ Class extends AlignCalcWorkerI> workerClass)
{
- for (AlignCalcWorkerI worker:canUpdate.toArray(new AlignCalcWorkerI[0]))
+
+ AlignCalcWorkerI[] workers;
+ synchronized (canUpdate)
+ {
+ workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
+ }
+ for (AlignCalcWorkerI worker : workers)
{
if (workerClass.equals(worker.getClass()))
{
@@ -162,39 +284,116 @@ public class AlignCalcManager implements AlignCalcManagerI
@Override
public List getRegisteredWorkersOfClass(
- Class workerClass)
+ Class extends AlignCalcWorkerI> workerClass)
{
- List workingClass=new ArrayList();
- for (AlignCalcWorkerI worker:canUpdate.toArray(new AlignCalcWorkerI[0]))
+ List workingClass = new ArrayList();
+ synchronized (canUpdate)
{
- if (workerClass.equals(worker.getClass()))
+ for (AlignCalcWorkerI worker : canUpdate)
{
- workingClass.add(worker);
+ if (workerClass.equals(worker.getClass()))
+ {
+ workingClass.add(worker);
+ }
}
}
- return (workingClass.size()==0) ? null : workingClass;
+ return (workingClass.size() == 0) ? null : workingClass;
}
@Override
- public boolean startRegisteredWorkersOfClass(Class workerClass)
+ public void enableWorker(AlignCalcWorkerI worker)
{
- List workers=getRegisteredWorkersOfClass(workerClass);
- if (workers==null)
- {
- return false;
- }
- for (AlignCalcWorkerI worker: workers) {
- startWorker(worker);
- }
- return true;
+ synchronized (blackList)
+ {
+ blackList.remove(worker.getClass());
+ }
}
@Override
- public void workerMayRun(AlignCalcWorkerI worker)
+ public void removeRegisteredWorkersOfClass(
+ Class extends AlignCalcWorkerI> typeToRemove)
{
- if (blackList.contains(worker.getClass()))
+ List removable = new ArrayList();
+ Set toremovannot = new HashSet();
+ synchronized (restartable)
{
+ for (AlignCalcWorkerI worker : restartable)
+ {
+ if (typeToRemove.equals(worker.getClass()))
+ {
+ removable.add(worker);
+ toremovannot.add(worker);
+ }
+ }
+ restartable.removeAll(removable);
+ }
+ synchronized (canUpdate)
+ {
+ for (AlignCalcWorkerI worker : canUpdate)
+ {
+ if (typeToRemove.equals(worker.getClass()))
+ {
+ removable.add(worker);
+ toremovannot.add(worker);
+ }
+ }
+ canUpdate.removeAll(removable);
+ }
+ // TODO: finish testing this extension
+
+ /*
+ * synchronized (inProgress) { // need to kill or mark as dead any running
+ * threads... (inProgress.get(typeToRemove)); }
+ *
+ * if (workers == null) { return; } for (AlignCalcWorkerI worker : workers)
+ * {
+ *
+ * if (isPending(worker)) { worker.abortAndDestroy(); startWorker(worker); }
+ * else { System.err.println("Pending exists for " + workerClass); } }
+ */
+ }
+
+ /**
+ * Deletes the worker that update the given annotation, provided it is marked
+ * as deletable.
+ */
+ @Override
+ public void removeWorkerForAnnotation(AlignmentAnnotation ann)
+ {
+ /*
+ * first just find those to remove (to avoid
+ * ConcurrentModificationException)
+ */
+ List toRemove = new ArrayList();
+ for (AlignCalcWorkerI worker : restartable)
+ {
+ if (worker.involves(ann))
+ {
+ if (worker.isDeletable())
+ {
+ toRemove.add(worker);
+ }
+ }
+ }
+
+ /*
+ * remove all references to deleted workers so any references
+ * they hold to annotation data can be garbage collected
+ */
+ for (AlignCalcWorkerI worker : toRemove)
+ {
+ restartable.remove(worker);
blackList.remove(worker.getClass());
+ inProgress.remove(worker);
+ canUpdate.remove(worker);
+ synchronized (updating)
+ {
+ List upd = updating.get(worker.getClass());
+ if (upd != null)
+ {
+ upd.remove(worker);
+ }
+ }
}
}
}