![](/img/trans.png)
[英]how to apply a multi threading to process List<MyDto> 5 dto objects at atime
[英]Threading the same object process with a list of many objects
我的问题是:控制多个“ RunnableObject
”列表的线程的“最佳”方法是什么?
我有一种处理多线程的方法,但是我认为它不是最佳的。 在很多情况下,我都有一个对象列表,每个对象都需要做同样的事情,但是我不希望它们一次执行一个对象,因此我将它们放在一个列表中并在其中放入一些对象一旦。 完成后,它将在控制器上调用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;
}
}
您刚刚重新发明了ThreadPoolExecutor !
由于您使用的是Java 7,因此很可能会使用新的fork join API,但是我很确定在您的情况下,您需要的是ThreadPoolExecutor。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.