class IteratorHelper { T data; Runnable r; boolean flag=false; public boolean doneflag=false; public IteratorHelper (Runnable r) { this.r = r; } public void start() { //Start the thread which runs the run() method which calls yield to produce the next value of the iterator new Thread(r).start(); } public synchronized void yield(T v) { //Check if the value has been consumed, if not then wait if(flag) { try { wait(); } catch(Exception ie) { ie.getMessage(); } } //If the value has been consumed by get(), set the next value data = v; flag = true; //Notify the main thread running the get() method System.out.println("Yield "+data); notify(); } public synchronized T get() { //Check if the producer has produced a value, if not wait try { if(!flag) { wait(); } } catch(InterruptedException ie) { ie.getMessage(); } flag=false; //Notify the thread yielding values to yield the next value notify(); //Return this value to the next() return data; } public void setDone() { doneflag = true; } public boolean done(){ //Yield has completed return doneflag; } }