Iterates over its process method and works within any actor which supports 2-way messages.
We can use JAIterator to calculate factorials:
final int max = 5; RP printResult = new RP() { public void process(Object rsp) { System.out.println(rsp); } }; (new JAIterator() { int i; int r = 1; public void process(RP rp) throws Exception { if (i >= max) rp.process(new Integer(r)); else { i += 1; r = r * i; rp.process(null); } } }).iterate(printResult);
The JAIterator.process method is called repeatedly until it returns a non-null response, which is then returned by JAIterator. However, the response need not be returned immediately, so the JAIterator.process method can send messages to other actors:
public class Factorial extends JLPCActor { public Factorial(Mailbox mailbox) { super(mailbox); } protected void processRequest(Object req, final RP rp) throws Exception { final int max = 5; RP printResult = new RP() { public void process(Object rsp) throws Exception { System.out.println(rsp); rp.process(null); } }; (new JAIterator() { int i; int r = 1; Multiplier mp = new Multiplier(getMailbox()); protected void process(RP rp) throws Exception { if (i >= max) rp.process(new Integer(r)); else { i += 1; Multiply m = new Multiply(); m.a = r; m.b = i; send(mp, m, new RP() { public void process(Object rsp) throws Exception { r = ((Integer) rsp).intValue(); } }); rp.process(null); } } }).iterate(printResult); } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|