没有匹配的构造函数构建actor系统,

时间:2019-07-06 22:28:30

标签: java akka

我正在尝试为蒙特卡洛pi计算实现Akka,但是在没有匹配的构造函数的情况下出现问题。无论如何,我可以改善主要方法吗?我可以拥有静态变量的值,但我想让它们具有输入。还有任何我可以改善此解决方案的方法,因为我也必须为Gregory-Leibniz实现它?

package com.actors.montecarlo;

import akka.actor.UntypedActor;

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

import akka.actor.Props;
import akka.actor.ActorRef;

public class Main {
    static class Data {
        public static int ACTOR_COUNT;
        public static int DARTS_PER_ACTOR;
        // Sum of the Dart Actor results.
        public static double sum = 0.0;
        // Keep track of actors still going.
        public static Integer actorsLeft = ACTOR_COUNT;
    }

    public class Dart extends UntypedActor {

        /**
         * Use Monte Carlo integration to approximate pi.
         */
        private float approximatePi(int NUMBER_OF_POINTS) {
            int inside = 0; // Keep track of points inside the circle.
            for (int i = 0; i < NUMBER_OF_POINTS; i++) {
                Point p = Point.genRandPoint();
                if (p.x * p.x + p.y * p.y <= 1) { // Check if point is inside circle.
                    inside += 1;
                }
            }
            return 4 * ((float) inside) / NUMBER_OF_POINTS;
        }

        @Override
        public void onReceive(Object msg) {
            if (msg != null) {
                getSender().tell(approximatePi(Data.DARTS_PER_ACTOR), getSelf());
            } else {
                getContext().stop(getSelf());
                unhandled(msg);
            }
        }
    }

    public static class Point {
        public double x;
        public double y;

        public Point(double x, double y) {
            this.x = x;
            this.y = y;
        }

        /**
         * Generates a random point in the unit square.
         * @return a point in the unit square
         */
        static public Point genRandPoint() {
            return new Point(
                    ThreadLocalRandom
                            .current()
                            .nextDouble(-1, 1),
                    ThreadLocalRandom
                            .current()
                            .nextDouble(-1, 1)
            );
        }
    }

    public class World extends UntypedActor {



        /**
         * Calculate the "average of the averages".
         * Each Dart actor approximates pi. This function takes those results
         * and averages them to get a *hopefully* better approximation of pi.
         * TODO: There is almost certainly a better reduction method.
         */
        public void onReceive(Object msg) {

            if (msg != null) {
                Data.sum += (Float) msg;
                Data.actorsLeft--;
                if (Data.actorsLeft == 0) {
                    System.out.println(Data.sum / Data.ACTOR_COUNT);
                    getContext().stop(getSelf());
                }
            } else {
                unhandled(msg);
            }
        }

        /**
         * Initializes a number of darts and tells the Dart actors
         * and tells them to start computing.
         */
        @Override
        public void preStart() {

            for (int i = 0; i < Data.ACTOR_COUNT; i++) {
                final ActorRef dart = getContext()
                        .actorOf(
                                Props.create(Dart.class),
                                "dart" + Integer.toString(i));
                // The choice of "0" is used, but anything non-null would
                // work. (If it were null, the Dart actor would die before
                // it did any work.
                dart.tell(0, getSelf());
            }
        }
    }

    public static void main(String[] args) {


        Scanner scannerObj = new Scanner(System.in);

        System.out.print("Insert number of actors: ");
        Data.ACTOR_COUNT = scannerObj.nextInt();
        System.out.println("Nº actors : " + Data.ACTOR_COUNT);

        System.out.print("Insert darts per actor: ");
        Data.DARTS_PER_ACTOR = scannerObj.nextInt();
        System.out.println("Darts: " + Data.DARTS_PER_ACTOR);
        /*
        long start = System.nanoTime();
        akka.Main.main(new String[] { World.class.getName() });
        long stop = System.nanoTime();
        long timeRes = (stop - start)/1_000_000_000;
        long ttTime = (stop - start) / ACTOR_COUNT;
        double ttTimeSeconds = ((double) ttTime / 1_000_000_000);*/

        akka.Main.main(new String[] { World.class.getName() });



    }
}


这是我收到的错误

Exception in thread "main" java.lang.IllegalArgumentException: no matching constructor found on class com.actors.montecarlo.Main$World for arguments []

在akka主线上

有帮助吗?

0 个答案:

没有答案