package org.mwg.core.task;

import org.mwg.task.TaskResultIterator;

class CoreTaskResultIterator<A> implements TaskResultIterator<A> {

    private final Object[] _backend;

    private final int _size;

    private int _current = 0;

    CoreTaskResultIterator(Object[] p_backend) {
        if (p_backend != null) {
            this._backend = p_backend;
        } else {
            _backend = new Object[0];
        }
        _size = _backend.length;
    }

    @Override
    public synchronized A next() {
        if (_current < _size) {
            Object result = _backend[_current];
            _current++;
            return (A) result;
        } else {
            return null;
        }
    }
}

@@ -62,16 +62,67 @@
 rator;%0A%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 class Co
@@ -218,17 +218,16 @@
 ackend;%0A
-%0A
     priv
@@ -247,17 +247,16 @@
  _size;%0A
-%0A
     priv
@@ -259,19 +259,35 @@
 private 
+f
 in
-t
+al AtomicInteger
  _curren
@@ -291,12 +291,8 @@
 rent
- = 0
 ;%0A%0A 
@@ -335,24 +335,65 @@
 _backend) %7B%0A
+        _current = new AtomicInteger(0);%0A
         if (
@@ -586,21 +586,8 @@
 lic 
-synchronized 
 A ne
@@ -605,103 +605,84 @@
     
-if (_current %3C _size) %7B%0A            Object result = _backend%5B_current%5D;%0A            _current++;
+final int cursor = _current.getAndIncrement();%0A        if (cursor %3C _size) %7B
 %0A   
@@ -705,14 +705,24 @@
 (A) 
-result
+_backend%5Bcursor%5D
 ;%0A  

optimize the parralel protection by using a CAS instead of a synchronized