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 2010/03/26 20:08:57 UTC

svn commit: r928022 - in /pivot/trunk/core: src/org/apache/pivot/util/concurrent/TaskGroup.java src/org/apache/pivot/util/concurrent/TaskSequence.java test/org/apache/pivot/util/concurrent/test/TaskTest.java

Author: gbrown
Date: Fri Mar 26 19:08:57 2010
New Revision: 928022

URL: http://svn.apache.org/viewvc?rev=928022&view=rev
Log:
Fix bug in TaskSequence (executing subtasks synchronously was not setting the result or fault members); make sequence/group methods synchronized in TaskSequence/TaskGroup.

Modified:
    pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskGroup.java
    pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskSequence.java
    pivot/trunk/core/test/org/apache/pivot/util/concurrent/test/TaskTest.java

Modified: pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskGroup.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskGroup.java?rev=928022&r1=928021&r2=928022&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskGroup.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskGroup.java Fri Mar 26 19:08:57 2010
@@ -30,7 +30,6 @@ import org.apache.pivot.util.ImmutableIt
 public class TaskGroup extends Task<Void>
     implements Group<Task<?>>, Iterable<Task<?>> {
     private HashSet<Task<?>> tasks = new HashSet<Task<?>>();
-    private int count = 0;
     private int complete = 0;
 
     public TaskGroup() {
@@ -67,7 +66,7 @@ public class TaskGroup extends Task<Void
             ((Task<Object>)task).execute(taskListener);
         }
 
-        while (complete < count) {
+        while (complete < getCount()) {
             try {
                 wait();
             } catch (InterruptedException exception) {
@@ -95,39 +94,29 @@ public class TaskGroup extends Task<Void
     }
 
     @Override
-    public boolean add(Task<?> element) {
+    public synchronized boolean add(Task<?> element) {
         if (isPending()) {
             throw new IllegalStateException();
         }
 
-        boolean added = tasks.add(element);
-        if (added) {
-            count++;
-        }
-
-        return added;
+        return tasks.add(element);
     }
 
     @Override
-    public boolean remove(Task<?> element) {
+    public synchronized boolean remove(Task<?> element) {
         if (isPending()) {
             throw new IllegalStateException();
         }
 
-        boolean removed = tasks.remove(element);
-        if (removed) {
-            count--;
-        }
-
-        return removed;
+        return tasks.remove(element);
     }
 
     @Override
-    public boolean contains(Task<?> element) {
+    public synchronized boolean contains(Task<?> element) {
         return tasks.contains(element);
     }
 
-    public int getCount() {
+    public synchronized int getCount() {
         return tasks.getCount();
     }
 

Modified: pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskSequence.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskSequence.java?rev=928022&r1=928021&r2=928022&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskSequence.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/concurrent/TaskSequence.java Fri Mar 26 19:08:57 2010
@@ -30,6 +30,7 @@ import org.apache.pivot.util.ImmutableIt
 public class TaskSequence extends Task<Void>
     implements Sequence<Task<?>>, Iterable<Task<?>> {
     private ArrayList<Task<?>> tasks = new ArrayList<Task<?>>();
+    private int complete = 0;
 
     public TaskSequence() {
         super();
@@ -40,20 +41,46 @@ public class TaskSequence extends Task<V
     }
 
     @Override
-    public Void execute() throws TaskExecutionException {
+    @SuppressWarnings("unchecked")
+    public synchronized Void execute() throws TaskExecutionException {
+        TaskListener<Object> taskListener = new TaskListener<Object>() {
+            @Override
+            public void taskExecuted(Task<Object> task) {
+                synchronized (TaskSequence.this) {
+                    complete++;
+                    TaskSequence.this.notify();
+                }
+            }
+
+            @Override
+            public void executeFailed(Task<Object> task) {
+                synchronized (TaskSequence.this) {
+                    complete++;
+                    TaskSequence.this.notify();
+                }
+            }
+        };
+
+        complete = 0;
         for (Task<?> task : tasks) {
             if (abort) {
                 throw new AbortException();
             }
 
-            task.execute();
+            ((Task<Object>)task).execute(taskListener);
+
+            try {
+                wait();
+            } catch (InterruptedException exception) {
+                throw new TaskExecutionException(exception);
+            }
         }
 
         return null;
     }
 
     @Override
-    public int add(Task<?> task) {
+    public synchronized int add(Task<?> task) {
         int index = tasks.getLength();
         insert(task, index);
 
@@ -61,7 +88,7 @@ public class TaskSequence extends Task<V
     }
 
     @Override
-    public void insert(Task<?> task, int index) {
+    public synchronized void insert(Task<?> task, int index) {
         if (isPending()) {
             throw new IllegalStateException();
         }
@@ -70,7 +97,7 @@ public class TaskSequence extends Task<V
     }
 
     @Override
-    public Task<?> update(int index, Task<?> task) {
+    public synchronized Task<?> update(int index, Task<?> task) {
         if (isPending()) {
             throw new IllegalStateException();
         }
@@ -79,7 +106,7 @@ public class TaskSequence extends Task<V
     }
 
     @Override
-    public int remove(Task<?> task) {
+    public synchronized int remove(Task<?> task) {
         int index = tasks.indexOf(task);
         if (index != -1) {
             tasks.remove(index, 1);
@@ -89,7 +116,7 @@ public class TaskSequence extends Task<V
     }
 
     @Override
-    public Sequence<Task<?>> remove(int index, int count) {
+    public synchronized Sequence<Task<?>> remove(int index, int count) {
         if (isPending()) {
             throw new IllegalStateException();
         }
@@ -98,17 +125,17 @@ public class TaskSequence extends Task<V
     }
 
     @Override
-    public Task<?> get(int index) {
+    public synchronized Task<?> get(int index) {
         return tasks.get(index);
     }
 
     @Override
-    public int indexOf(Task<?> task) {
+    public synchronized int indexOf(Task<?> task) {
         return tasks.indexOf(task);
     }
 
     @Override
-    public int getLength() {
+    public synchronized int getLength() {
         return tasks.getLength();
     }
 

Modified: pivot/trunk/core/test/org/apache/pivot/util/concurrent/test/TaskTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/util/concurrent/test/TaskTest.java?rev=928022&r1=928021&r2=928022&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/util/concurrent/test/TaskTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/util/concurrent/test/TaskTest.java Fri Mar 26 19:08:57 2010
@@ -22,7 +22,6 @@ import org.apache.pivot.util.concurrent.
 import org.apache.pivot.util.concurrent.TaskSequence;
 import org.junit.Test;
 
-
 public class TaskTest {
     public static class SleepTask extends Task<Void> {
         private long timeout = 0;
@@ -70,13 +69,13 @@ public class TaskTest {
 
         TaskSequence taskSequence = new TaskSequence();
 
-        SleepTask task1 = new SleepTask(2000);
+        SleepTask task1 = new SleepTask(500);
         taskSequence.add(task1);
 
-        SleepTask task2 = new SleepTask(500);
+        SleepTask task2 = new SleepTask(1000);
         taskSequence.add(task2);
 
-        SleepTask task3 = new SleepTask(1000);
+        SleepTask task3 = new SleepTask(2000);
         taskSequence.add(task3);
 
         synchronized (taskListener) {
@@ -107,13 +106,13 @@ public class TaskTest {
 
         TaskGroup taskGroup = new TaskGroup();
 
-        SleepTask task1 = new SleepTask(2000);
+        SleepTask task1 = new SleepTask(500);
         taskGroup.add(task1);
 
-        SleepTask task2 = new SleepTask(500);
+        SleepTask task2 = new SleepTask(1000);
         taskGroup.add(task2);
 
-        SleepTask task3 = new SleepTask(1000);
+        SleepTask task3 = new SleepTask(2000);
         taskGroup.add(task3);
 
         synchronized (taskListener) {