Updated after comments, see #1278
This commit is contained in:
parent
c1f9e764a9
commit
1b3ee08287
9 changed files with 8 additions and 21 deletions
|
|
@ -16,8 +16,6 @@ import java.util.concurrent.CountDownLatch;
|
|||
|
||||
public class Pi {
|
||||
|
||||
private static final ActorSystem system = ActorSystem.apply();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Pi pi = new Pi();
|
||||
pi.calculate(4, 10000, 10000);
|
||||
|
|
@ -113,18 +111,17 @@ public class Pi {
|
|||
};
|
||||
LinkedList<ActorRef> actors = new LinkedList<ActorRef>() {
|
||||
{
|
||||
for (int i = 0; i < nrOfWorkers; i++) add(system.actorOf(Worker.class));
|
||||
for (int i = 0; i < nrOfWorkers; i++) add(context().actorOf(Worker.class));
|
||||
}
|
||||
};
|
||||
RoutedProps props = new RoutedProps(routerCreator, new LocalConnectionManager(actors), new akka.actor.Timeout(-1), true);
|
||||
router = new RoutedActorRef(system, props, getSelf(), "pi");
|
||||
router = new RoutedActorRef(system(), props, getSelf(), "pi");
|
||||
}
|
||||
|
||||
// message handler
|
||||
public void onReceive(Object message) {
|
||||
|
||||
if (message instanceof Calculate) {
|
||||
|
||||
// schedule work
|
||||
for (int start = 0; start < nrOfMessages; start++) {
|
||||
router.tell(new Work(start, nrOfElements), getSelf());
|
||||
|
|
@ -161,6 +158,7 @@ public class Pi {
|
|||
// ==================
|
||||
public void calculate(final int nrOfWorkers, final int nrOfElements, final int nrOfMessages)
|
||||
throws Exception {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
// this latch is only plumbing to know when the calculation is completed
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
|
|
|||
|
|
@ -9,8 +9,6 @@ import akka.actor.{ ActorSystemImpl, Actor, ActorSystem }
|
|||
|
||||
object Pi extends App {
|
||||
|
||||
val system = ActorSystem()
|
||||
|
||||
// Initiate the calculation
|
||||
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
|
||||
|
||||
|
|
@ -87,14 +85,11 @@ object Pi extends App {
|
|||
}
|
||||
}
|
||||
|
||||
object Master {
|
||||
val impl = system.asInstanceOf[ActorSystemImpl]
|
||||
}
|
||||
|
||||
// ==================
|
||||
// ===== Run it =====
|
||||
// ==================
|
||||
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
|
||||
val system = ActorSystem()
|
||||
|
||||
// this latch is only plumbing to know when the calculation is completed
|
||||
val latch = new CountDownLatch(1)
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ class WorkerSpec extends WordSpec with MustMatchers {
|
|||
val testActor = TestActorRef[Worker]
|
||||
val actor = testActor.underlyingActor
|
||||
actor.calculatePiFor(0, 0) must equal(0.0)
|
||||
actor.calculatePiFor(1, 1) must equal(-1.3333333333333333)
|
||||
actor.calculatePiFor(1, 1) must be(-1.3333333333333333 plusOrMinus 0.0000000001)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue