54 lines
1.7 KiB
Java
54 lines
1.7 KiB
Java
package org.syndicate_lang.actors.example.example2;
|
|
|
|
import org.syndicate_lang.actors.Actor;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class Main implements IForwarder {
|
|
|
|
private List<IForwarder> _actors;
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
Actor.convenientLogging();
|
|
Actor.forObject(new Main(1000000, 10)).syncVoid(Main::boot).await();
|
|
Actor.awaitAll();
|
|
}
|
|
|
|
public final int _nActors;
|
|
public final int _nRounds;
|
|
public int _remainingToReceive;
|
|
|
|
public Main(int nActors, int nRounds) {
|
|
this._nActors = nActors;
|
|
this._nRounds = nRounds;
|
|
this._remainingToReceive = nActors;
|
|
}
|
|
|
|
public void boot() {
|
|
this._actors = new ArrayList<>();
|
|
IForwarder me = Actor.ref(this).asyncProxy(IForwarder.class);
|
|
for (int i = 0; i < _nActors; i++) {
|
|
this._actors.add(Actor.forObject(
|
|
new Forwarder(i, this._actors, me, this._nRounds))
|
|
.asyncProxy(IForwarder.class));
|
|
// Actor.log().info(this._actors.get(this._actors.size()-1).toString());
|
|
}
|
|
Actor.log().info("Start");
|
|
this._actors.forEach((a) -> a.handleMessage(0));
|
|
}
|
|
|
|
@Override
|
|
public void handleMessage(int hopCount) {
|
|
this._remainingToReceive--;
|
|
// Actor.log().info(String.format("hopCount: %d, remainingToReceive: %d",
|
|
// hopCount,
|
|
// this._remainingToReceive));
|
|
if (this._remainingToReceive == 0) {
|
|
this._actors.forEach(IForwarder::shutdown);
|
|
this.shutdown();
|
|
Actor.log().info("Stop after " + (_nActors * _nRounds) + " messages");
|
|
}
|
|
}
|
|
}
|