使用许多对象的列表线程化相同的对象进程

时间:2012-06-06 23:32:58

标签: java multithreading optimization

我的问题是:控制线程列出多个“RunnableObject s”的“最佳”方法是什么?

我有一种方法可以接近多线程,但我不认为它是最优的。在很多情况下,我有一个对象列表,它们都需要做同样的事情,但我不希望它们一次只做一个,所以我会将它们放在一个列表中并将它们中的一些放在一旦。当一个完成后,它会在控制器上调用updateList()方法,该方法将删除已完成的项目并调用新项目。我认为这不是一个很好的方法。我想使用像.notify()这样的东西,但我不确定它在这种情况下是如何工作的(指导我到一个好的网站学习可能会有用)。

我的控制器类基本概述如下。 我试图让javadoc变得有用,所以如果你正在浏览,请继续阅读。感谢。

只是一些示例代码,让您了解我在说什么。

package controller;

import java.util.ArrayList;
import java.util.List;
import model.RunnableObject;

public class RunnableObjectController {

  private static RunnableObjectController instance;
  private List<RunnableObject> runnableObjects = new ArrayList<>();
  private List<RunnableObject> queuedRunnableObjects = new ArrayList<>();
  private List<RunnableObject> currentRunnableObjects = new ArrayList<>();
  private int limit = 10;

  private RunnableObjectController() {
  }

  public static RunnableObjectController getInstance() {
    if (instance == null) {
      instance = new RunnableObjectController();
    }
    return instance;
  }

  /**
  * Updates the list.
  */
  public void begin() {
    updateList();
  }

  /**
   * Called by a runnableObject when it's finished with its process
   */
  public synchronized void updateList() {
    removeFinishedRunnableObjects();
    addNewRunnableObjects();
    if (isFinished()) {
      MainController.getInstance().finish();
    }
  }

  /**
  * Searches the currentRunnableObjects for runnableObjects which are not in progress and removes them from the currentRunnableObjects.
  * It will add them to the queuedRunnableObjects if they are of status WAITING.
  */
  private void removeFinishedRunnableObjects() {
    int i = 0;
    while (i < currentRunnableObjects.size()) {
      RunnableObject runnableObject = currentRunnableObjects.get(i);
      if (runnableObject.getStatus() != RunnableObject.IN_PROGRESS) {
        currentRunnableObjects.remove(runnableObject);
        if (runnableObject.getStatus() == RunnableObject.WAITING) {
          queuedRunnableObjects.add(runnableObject);
        }
      } else {
        i++;
      }
    }
  }

  /**
  * Searches the queuedRunnableObjects and adds them to the currentRunnableObjects while the size of the currentRunnableObjects is less than the limit.
  * Begins the runnableObject in a new thread and sets its status to IN_PROGRESS
  */
  private void addNewRunnableObjects() {
    while (!queuedRunnableObjects.isEmpty() && currentRunnableObjects.size() < limit) {
      RunnableObject runnableObject = queuedRunnableObjects.get(0);
      if (runnableObject.getStatus() == RunnableObject.WAITING) {
        addCurrentRemoveQueued(runnableObject);
        new Thread(runnableObject).start();
        runnableObject.setStatus(RunnableObject.IN_PROGRESS);
      }
    }
  }

  /**
  * Adds the runnableObject to the currentThreadsObjects and removes it from the queuedRunnableObjects
  */
  private synchronized void addCurrentRemoveQueued(RunnableObject runnableObject) {
    currentRunnableObjects.add(runnableObject);
    queuedRunnableObjects.remove(runnableObject);
  }

  /**
   * Checks whether the current and queued notification lists are empty. Returns true if they both are.
   *
   * @return
   */
  private boolean isFinished() {
    if (queuedRunnableObjects.isEmpty() && currentRunnableObjects.isEmpty()) {
      return true;
    }
    return false;
  }
}

1 个答案:

答案 0 :(得分:5)

您刚刚重新发明了ThreadPoolExecutor

由于您使用的是java 7,因此很有可能使用新的fork join API,但我很确定在您的情况下ThreadPoolExecutor就是您所需要的。