JAL-961, JAL-1115 - pending test to ensure only one worker waits around for another...
[jalview.git] / src / jalview / workers / ConsensusThread.java
index 7dd25f8..9bdd714 100644 (file)
@@ -19,30 +19,37 @@ public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI
     super(alignViewport, alignPanel);
   }
 
+  @Override
   public void run()
   {
+    if (calcMan.isPending(this))
+    {
+      return;
+    }
+    calcMan.notifyStart(this);
+    long started=System.currentTimeMillis();
     try
     {
       AlignmentAnnotation consensus = alignViewport.getAlignmentConsensusAnnotation();
-      if (consensus==null) { return; 
+      if (consensus==null || calcMan.isPending(this))  {
+        calcMan.workerComplete(this);
+        return;
       }
-      calcMan.notifyStart(this); 
-      while (!calcMan.notifyWorking(this)) 
+      while (!calcMan.notifyWorking(this))
       {
+        // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
         try
         {
           if (ap != null)
           {
-            ap.paintAlignment(false);
+           ap.paintAlignment(false);
           }
-
           Thread.sleep(200);
         } catch (Exception ex)
         {
           ex.printStackTrace();
         }
       }
-      calcMan.notifyWorking(this);
       if (alignViewport.isClosed())
       {
         abortAndDestroy();
@@ -66,8 +73,17 @@ public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI
       consensus.annotations = new Annotation[aWidth];
       Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
       hconsensus = new Hashtable[aWidth];
-      AAFrequency.calculate(alignment.getSequencesArray(), 0,
+      try {
+       AAFrequency.calculate(alignment.getSequencesArray(), 0,
               alignment.getWidth(), hconsensus, true);
+      } catch (ArrayIndexOutOfBoundsException x){
+        // this happens due to a race condition -
+        // alignment was edited at same time as calculation was running
+        //
+//        calcMan.workerCannotRun(this);
+        calcMan.workerComplete(this);
+        return;
+      }
       alignViewport.setSequenceConsensusHash(hconsensus);
       updateResultAnnotation(true);
       ColourSchemeI globalColourScheme = alignViewport
@@ -97,6 +113,7 @@ public class ConsensusThread extends AlignCalcWorker implements AlignCalcWorkerI
    * update the consensus annotation from the sequence profile data using
    * current visualization settings.
    */
+  @Override
   public void updateAnnotation()
   {
     updateResultAnnotation(false);