You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/06/02 18:16:19 UTC
svn commit: r781074 - in
/incubator/pivot/trunk/core/src/pivot/util/concurrent: TaskGroup.java
TaskSequence.java
Author: gbrown
Date: Tue Jun 2 16:16:19 2009
New Revision: 781074
URL: http://svn.apache.org/viewvc?rev=781074&view=rev
Log:
Clean up TaskGroup and TaskSequence implementations.
Modified:
incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskGroup.java
incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskSequence.java
Modified: incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskGroup.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskGroup.java?rev=781074&r1=781073&r2=781074&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskGroup.java (original)
+++ incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskGroup.java Tue Jun 2 16:16:19 2009
@@ -19,7 +19,7 @@
import java.util.Iterator;
import pivot.collections.Group;
-import pivot.collections.HashMap;
+import pivot.collections.HashSet;
import pivot.util.ImmutableIterator;
/**
@@ -31,24 +31,9 @@
*/
public class TaskGroup extends Task<Void>
implements Group<Task<?>>, Iterable<Task<?>> {
- private class TaskHandler implements TaskListener<Object> {
- public void taskExecuted(Task<Object> task) {
- synchronized (TaskGroup.this) {
- tasks.put(task, Boolean.TRUE);
- TaskGroup.this.notify();
- }
- }
-
- public void executeFailed(Task<Object> task) {
- synchronized (TaskGroup.this) {
- tasks.put(task, Boolean.TRUE);
- TaskGroup.this.notify();
- }
- }
- }
-
- private HashMap<Task<?>, Boolean> tasks = new HashMap<Task<?>, Boolean>();
- private boolean executing = false;
+ private HashSet<Task<?>> tasks = new HashSet<Task<?>>();
+ private int count = 0;
+ private int complete = 0;
public TaskGroup() {
super();
@@ -61,61 +46,58 @@
@Override
@SuppressWarnings("unchecked")
public synchronized Void execute() throws TaskExecutionException {
- executing = true;
-
- try {
- TaskHandler taskHandler = new TaskHandler();
-
- for (Task<?> task : tasks) {
- tasks.put(task, Boolean.FALSE);
- ((Task<Object>)task).execute(taskHandler);
+ TaskListener<Object> taskListener = new TaskListener<Object>() {
+ public void taskExecuted(Task<Object> task) {
+ synchronized(TaskGroup.this) {
+ complete++;
+ TaskGroup.this.notify();
+ }
}
- boolean complete = false;
-
- while (!complete) {
- try {
- wait();
- } catch (InterruptedException exception) {
- throw new TaskExecutionException(exception);
+ public void executeFailed(Task<Object> task) {
+ synchronized(TaskGroup.this) {
+ complete++;
+ TaskGroup.this.notify();
}
+ }
+ };
- complete = true;
- for (Task<?> task : tasks) {
- if (!tasks.get(task)) {
- complete = false;
- break;
- }
- }
+ complete = 0;
+ for (Task<?> task : tasks) {
+ ((Task<Object>)task).execute(taskListener);
+ }
+
+ while (complete < count) {
+ try {
+ wait();
+ } catch (InterruptedException exception) {
+ throw new TaskExecutionException(exception);
}
- } finally {
- executing = false;
}
return null;
}
- @SuppressWarnings("unchecked")
- public synchronized void add(Task<?> element) {
- if (executing) {
- throw new IllegalStateException("Task group is executing.");
+ public void add(Task<?> element) {
+ if (isPending()) {
+ throw new IllegalStateException();
}
- tasks.put((Task<Object>)element, Boolean.FALSE);
+ tasks.add(element);
+ count++;
}
- @SuppressWarnings("unchecked")
- public synchronized void remove(Task<?> element) {
- if (executing) {
- throw new IllegalStateException("Task group is executing.");
+ public void remove(Task<?> element) {
+ if (isPending()) {
+ throw new IllegalStateException();
}
- tasks.remove((Task<Object>)element);
+ tasks.remove(element);
+ count--;
}
- @SuppressWarnings("unchecked")
public boolean contains(Task<?> element) {
- return tasks.containsKey((Task<Object>)element);
+ return tasks.contains(element);
}
public boolean isEmpty() {
Modified: incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskSequence.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskSequence.java?rev=781074&r1=781073&r2=781074&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskSequence.java (original)
+++ incubator/pivot/trunk/core/src/pivot/util/concurrent/TaskSequence.java Tue Jun 2 16:16:19 2009
@@ -24,16 +24,13 @@
/**
* Class that runs a sequence of tasks in series and notifies listeners
- * when all tasks are complete. Callers can retrieve task results or faults by
- * calling {@link Task#getResult()} and {@link Task#getFault()},
- * respectively.
+ * when all tasks are complete.
*
* @author gbrown
*/
public class TaskSequence extends Task<Void>
implements Sequence<Task<?>>, Iterable<Task<?>> {
private ArrayList<Task<?>> tasks = new ArrayList<Task<?>>();
- private int activeTaskIndex = -1;
public TaskSequence() {
super();
@@ -45,38 +42,11 @@
@Override
@SuppressWarnings("unchecked")
- public synchronized Void execute() throws TaskExecutionException {
- TaskListener<Object> taskListener = new TaskListener<Object>() {
- public void taskExecuted(Task<Object> task) {
- synchronized (TaskSequence.this) {
- TaskSequence.this.notify();
- }
- }
-
- public void executeFailed(Task<Object> task) {
- synchronized (TaskSequence.this) {
- TaskSequence.this.notify();
- }
- }
- };
-
- activeTaskIndex = 0;
-
- while (activeTaskIndex < tasks.getLength()) {
- Task<Object> activeTask = (Task<Object>)tasks.get(activeTaskIndex);
- activeTask.execute(taskListener);
-
- try {
- wait();
- } catch (InterruptedException exception) {
- throw new TaskExecutionException(exception);
- }
-
- activeTaskIndex++;
+ public Void execute() throws TaskExecutionException {
+ for (Task<?> task : tasks) {
+ task.execute();
}
- activeTaskIndex = -1;
-
return null;
}
@@ -87,16 +57,16 @@
return index;
}
- public synchronized void insert(Task<?> task, int index) {
- if (activeTaskIndex != -1) {
+ public void insert(Task<?> task, int index) {
+ if (isPending()) {
throw new IllegalStateException();
}
tasks.insert(task, index);
}
- public synchronized Task<?> update(int index, Task<?> task) {
- if (activeTaskIndex != -1) {
+ public Task<?> update(int index, Task<?> task) {
+ if (isPending()) {
throw new IllegalStateException();
}
@@ -112,8 +82,8 @@
return index;
}
- public synchronized Sequence<Task<?>> remove(int index, int count) {
- if (activeTaskIndex != -1) {
+ public Sequence<Task<?>> remove(int index, int count) {
+ if (isPending()) {
throw new IllegalStateException();
}