minor cleanup
This commit is contained in:
parent
706e128d15
commit
c26879fcb6
1 changed files with 14 additions and 13 deletions
|
|
@ -4,17 +4,20 @@
|
||||||
|
|
||||||
package akka.tutorial.java.first;
|
package akka.tutorial.java.first;
|
||||||
|
|
||||||
import akka.actor.*;
|
import static akka.actor.Actors.actorOf;
|
||||||
import static akka.actor.Actors.*;
|
import static akka.actor.Actors.poisonPill;
|
||||||
|
|
||||||
import akka.routing.*;
|
|
||||||
import static akka.routing.Routing.Broadcast;
|
|
||||||
|
|
||||||
import akka.dispatch.Dispatchers;
|
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
import akka.actor.ActorRef;
|
||||||
|
import akka.actor.UntypedActor;
|
||||||
|
import akka.actor.UntypedActorFactory;
|
||||||
|
import akka.routing.CyclicIterator;
|
||||||
|
import akka.routing.InfiniteIterator;
|
||||||
|
import akka.routing.Routing.Broadcast;
|
||||||
|
import akka.routing.UntypedLoadBalancer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* First part in Akka tutorial for Java.
|
* First part in Akka tutorial for Java.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|
@ -82,7 +85,7 @@ public class Pi {
|
||||||
|
|
||||||
// define the work
|
// define the work
|
||||||
private double calculatePiFor(int arg, int nrOfElements) {
|
private double calculatePiFor(int arg, int nrOfElements) {
|
||||||
double acc = 0.0D;
|
double acc = 0.0;
|
||||||
for (int i = arg * nrOfElements; i <= ((arg + 1) * nrOfElements - 1); i++) {
|
for (int i = arg * nrOfElements; i <= ((arg + 1) * nrOfElements - 1); i++) {
|
||||||
acc += 4 * Math.pow(-1, i) / (2 * i + 1);
|
acc += 4 * Math.pow(-1, i) / (2 * i + 1);
|
||||||
}
|
}
|
||||||
|
|
@ -92,7 +95,7 @@ public class Pi {
|
||||||
// message handler
|
// message handler
|
||||||
public void onReceive(Object message) {
|
public void onReceive(Object message) {
|
||||||
if (message instanceof Work) {
|
if (message instanceof Work) {
|
||||||
Work work = (Work)message;
|
Work work = (Work) message;
|
||||||
getContext().replyUnsafe(new Result(calculatePiFor(work.getArg(), work.getNrOfElements()))); // perform the work
|
getContext().replyUnsafe(new Result(calculatePiFor(work.getArg(), work.getNrOfElements()))); // perform the work
|
||||||
} else throw new IllegalArgumentException("Unknown message [" + message + "]");
|
} else throw new IllegalArgumentException("Unknown message [" + message + "]");
|
||||||
}
|
}
|
||||||
|
|
@ -102,7 +105,6 @@ public class Pi {
|
||||||
// ===== Master =====
|
// ===== Master =====
|
||||||
// ==================
|
// ==================
|
||||||
static class Master extends UntypedActor {
|
static class Master extends UntypedActor {
|
||||||
private final int nrOfWorkers;
|
|
||||||
private final int nrOfMessages;
|
private final int nrOfMessages;
|
||||||
private final int nrOfElements;
|
private final int nrOfElements;
|
||||||
private final CountDownLatch latch;
|
private final CountDownLatch latch;
|
||||||
|
|
@ -126,7 +128,6 @@ public class Pi {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Master(int nrOfWorkers, int nrOfMessages, int nrOfElements, CountDownLatch latch) {
|
public Master(int nrOfWorkers, int nrOfMessages, int nrOfElements, CountDownLatch latch) {
|
||||||
this.nrOfWorkers = nrOfWorkers;
|
|
||||||
this.nrOfMessages = nrOfMessages;
|
this.nrOfMessages = nrOfMessages;
|
||||||
this.nrOfElements = nrOfElements;
|
this.nrOfElements = nrOfElements;
|
||||||
this.latch = latch;
|
this.latch = latch;
|
||||||
|
|
@ -163,7 +164,7 @@ public class Pi {
|
||||||
} else if (message instanceof Result) {
|
} else if (message instanceof Result) {
|
||||||
|
|
||||||
// handle result from the worker
|
// handle result from the worker
|
||||||
Result result = (Result)message;
|
Result result = (Result) message;
|
||||||
pi += result.getValue();
|
pi += result.getValue();
|
||||||
nrOfResults += 1;
|
nrOfResults += 1;
|
||||||
if (nrOfResults == nrOfMessages) getContext().stop();
|
if (nrOfResults == nrOfMessages) getContext().stop();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue