//Ryan Goodfellow CS580 Final public class IteratorHelper { I place; //The current index Itr goose; //The object under iteration Thread t; //The thread being used for the iteration I last; //The last index used for yielding an element public void start() { last = null; //initialize the most recent item produced to nil t = new Thread(goose); //create a new thread for iteration t.start(); //start the iteration thread } public IteratorHelper (Itr g) { this.goose = g; // Init member vars this.place = g.min(); } public void yield(I i) { this.place = i; //update the current place t.suspend(); //suspend the iteration thread } public synchronized V get() { // It is possible that the iterator thread has not had a chance yet to iterate //if this is the case wait for it while(last != null && last == place) { t.resume(); Thread.yield(); } last = place; //update the last used index V i = goose.elementAt(place); //get the value at the current index t.resume(); //resume the iterator thread Thread.yield(); //Try to yield to the iterator thread to let it do it's thing return i; //return the value at the current index of the object under iteration } public boolean done() { //We are done when we have reached the maximum value of the object under iteration return place.equals(goose.max()); } }