1

有五种从属算法和一种主算法。在每个主算法的迭代中,这五个从属算法并行工作(它们都实现了Runnable接口),当其中一个完成时,它会通知其他算法以便它们也终止,并且在所有这些完成后,主算法开始后处理。通知过程基于观察者模式。每个从属算法都实现Terminatable了接口,并有一个指向一个TerminationObserver类的链接,该类包含一个可运行的列表并具有这样一个方法:

 public synchronized void terminateAll() {
    for (Terminatable terminatable : terminatables) {
        if (!terminatable.isTerminated()) {
            terminatable.terminate();
        }
    }
 }

每个从属算法都是一组迭代,因此通过设置一个terminated布尔变量来执行终止,该变量true是停止迭代条件的一部分。下面是从属算法类的概述:

public class SlaveAlgorithm {

   /**
    * Process the algorithm.
    */    
   public void run() {
       try {
           threadBarrier.await();

           while (!stopConditionMet()) {
               performIteration()
           }

           // whether it is terminated by other algorithm or
           // the stop conditions met
           if (!isTerminated()) {  
              terminate();
              terminateOthers();
           }

       } catch (Exception e) {
        throw new RuntimeException(new AlgException(e));
       }
   }

   /**
    * Tests whether the algorithms is terminated.
    */
   public boolean isTerminated() {
       return terminated;
   }

   /**
    * Terminates the algorithm execution and
    * causes other algorithms to terminate as well.
    */
   public void terminate() {
       terminated = true;
   }

   /**
    * Removes the current algorithm form the termination observer's list
    * and requests the termination of other algorithms, left in the termination    observer.
    */
   private void terminateOthers() {
       terminationObserver.remove(this); // eliminate the already terminated process from the list
       terminationObserver.terminateAll();
   }
}

一切正常,但这似乎不是“最佳实践”。可能有一些我看不到的陷阱,或者可能有一些其他众所周知的做法来做我需要的事情?

4

1 回答 1

1

您应该将terminateOthers()决策留给 theTerminationObserver而不是SlaveAlgorithm。你应该有这样的东西:

public class SlaveAlgorithm {
  public void run() {
    try {
      threadBarrier.await();

      while (!stopConditionMet() && !isTerminated()) {
        performIteration()
      }
      done();
    }
    catch ...
  }

  /**
   * If not terminated notify observer the processing is done.
   */
  private void done() {
    if (!isTerminated()) {  
      terminationObserver.notifyDone(this)
    }
  }

观察者应该这样做:

public class TerminationObserverImpl {
  private void notifyDone(SlaveAlgorithm slave) {
    remove(slave); // Remove it from the list.
    terminateAll(); // Go through the list and terminate each slave.
  }
  ...
于 2013-04-13T17:07:36.633 回答