Updated after comments, see #1278
This commit is contained in:
parent
c1f9e764a9
commit
1b3ee08287
9 changed files with 8 additions and 21 deletions
|
|
@ -19,8 +19,6 @@ Here is an example. First type 'sbt' to start SBT interactively, the run 'update
|
||||||
|
|
||||||
> % sbt
|
> % sbt
|
||||||
|
|
||||||
> > update
|
|
||||||
|
|
||||||
> > project akka-sample-ants
|
> > project akka-sample-ants
|
||||||
|
|
||||||
> > run
|
> > run
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@ Here is an example. First type 'sbt' to start SBT interactively, the run 'update
|
||||||
|
|
||||||
> % sbt
|
> % sbt
|
||||||
|
|
||||||
> > update
|
|
||||||
|
|
||||||
> > project akka-sample-fsm
|
> > project akka-sample-fsm
|
||||||
|
|
||||||
> > run
|
> > run
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,7 @@ class Hakker(name: String, left: ActorRef, right: ActorRef) extends Actor {
|
||||||
object DiningHakkers {
|
object DiningHakkers {
|
||||||
val system = ActorSystem()
|
val system = ActorSystem()
|
||||||
|
|
||||||
def main(args: Array[String]) {
|
def main(args: Array[String]): Unit = {
|
||||||
run
|
run
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -168,7 +168,7 @@ object DiningHakkersOnFsm {
|
||||||
|
|
||||||
val system = ActorSystem()
|
val system = ActorSystem()
|
||||||
|
|
||||||
def main(args: Array[String]) {
|
def main(args: Array[String]): Unit = {
|
||||||
run
|
run
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@ Here is an example. First type 'sbt' to start SBT interactively, the run 'update
|
||||||
|
|
||||||
> % sbt
|
> % sbt
|
||||||
|
|
||||||
> > update
|
|
||||||
|
|
||||||
> > project akka-sample-hello
|
> > project akka-sample-hello
|
||||||
|
|
||||||
> > run
|
> > run
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import akka.actor.{ ActorSystem, Actor }
|
||||||
case object Start
|
case object Start
|
||||||
|
|
||||||
object Main {
|
object Main {
|
||||||
def main(args: Array[String]) {
|
def main(args: Array[String]): Unit = {
|
||||||
val system = ActorSystem()
|
val system = ActorSystem()
|
||||||
system.actorOf[HelloActor] ! Start
|
system.actorOf[HelloActor] ! Start
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@ import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
public class Pi {
|
public class Pi {
|
||||||
|
|
||||||
private static final ActorSystem system = ActorSystem.apply();
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Pi pi = new Pi();
|
Pi pi = new Pi();
|
||||||
pi.calculate(4, 10000, 10000);
|
pi.calculate(4, 10000, 10000);
|
||||||
|
|
@ -113,18 +111,17 @@ public class Pi {
|
||||||
};
|
};
|
||||||
LinkedList<ActorRef> actors = new LinkedList<ActorRef>() {
|
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);
|
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
|
// message handler
|
||||||
public void onReceive(Object message) {
|
public void onReceive(Object message) {
|
||||||
|
|
||||||
if (message instanceof Calculate) {
|
if (message instanceof Calculate) {
|
||||||
|
|
||||||
// schedule work
|
// schedule work
|
||||||
for (int start = 0; start < nrOfMessages; start++) {
|
for (int start = 0; start < nrOfMessages; start++) {
|
||||||
router.tell(new Work(start, nrOfElements), getSelf());
|
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)
|
public void calculate(final int nrOfWorkers, final int nrOfElements, final int nrOfMessages)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
final ActorSystem system = ActorSystem.create();
|
||||||
|
|
||||||
// this latch is only plumbing to know when the calculation is completed
|
// this latch is only plumbing to know when the calculation is completed
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,6 @@ import akka.actor.{ ActorSystemImpl, Actor, ActorSystem }
|
||||||
|
|
||||||
object Pi extends App {
|
object Pi extends App {
|
||||||
|
|
||||||
val system = ActorSystem()
|
|
||||||
|
|
||||||
// Initiate the calculation
|
// Initiate the calculation
|
||||||
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
|
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
|
||||||
|
|
||||||
|
|
@ -87,14 +85,11 @@ object Pi extends App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object Master {
|
|
||||||
val impl = system.asInstanceOf[ActorSystemImpl]
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================
|
// ==================
|
||||||
// ===== Run it =====
|
// ===== Run it =====
|
||||||
// ==================
|
// ==================
|
||||||
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
|
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
|
||||||
|
val system = ActorSystem()
|
||||||
|
|
||||||
// this latch is only plumbing to know when the calculation is completed
|
// this latch is only plumbing to know when the calculation is completed
|
||||||
val latch = new CountDownLatch(1)
|
val latch = new CountDownLatch(1)
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ class WorkerSpec extends WordSpec with MustMatchers {
|
||||||
val testActor = TestActorRef[Worker]
|
val testActor = TestActorRef[Worker]
|
||||||
val actor = testActor.underlyingActor
|
val actor = testActor.underlyingActor
|
||||||
actor.calculatePiFor(0, 0) must equal(0.0)
|
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