简体   繁体   中英

Akka actor tutorial in Scala to multithreading in Java

I tried to convert the Getting-started Akka tutorial from Scala to Java and replace actors with threads.

The tutorial can be found here http://typesafe.com/resources/tutorials/getting-started-with-akka-scala.html

The original Pi.scala file can be checked out from Typesafe giter8 template repository

g8 typesafehub/akka-first-tutorial-scala

I would like to know if what I did is correct.

And this is my Pi.java

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class Pi {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Listener listener = new Listener();

        //calculate(nrOfWorkers = 10, nrOfElements = 10000, nrOfMessages = 10000)
        Master master = new Master(4, 1000, 1000, listener);
        master.calculate();
    }
}

class Worker implements Callable<Double> {

    private int start;
    private int nrOfElements;

    public Worker(int start, int nrOfElements) {
        this.start = start;
        this.nrOfElements = nrOfElements;
    }

    @Override
    public Double call() throws Exception {
        double acc = 0.0;

        for(int i = start; i < start + nrOfElements; i++) {
            acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
        }
        return acc;
    }
}

class Listener {

    public void PiApproximation(double pi, long duration) {
        System.out.println(String.format("\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s", pi, duration));
    }

}

class Master {

    private double pi = 0.0;
    private int nrOfResults = 0;
    private final long start = java.lang.System.currentTimeMillis();

    private int nrOfWorkers;
    private int nrOfMessages;
    private int nrOfElements;
    private Listener listener;

    private ExecutorService executor;

    public Master(int nrOfWorkers, 
                  int nrOfMessages, 
                  int nrOfElements, 
                  Listener listener) {

        this.nrOfWorkers = nrOfWorkers;
        this.nrOfMessages = nrOfMessages;
        this.nrOfElements = nrOfElements;
        this.listener = listener;

        //Round robin scheduling is not enforced here as in akka.
        executor = Executors.newFixedThreadPool(nrOfWorkers);
    }

    public void calculate() {
        List<Future<Double>> list = new ArrayList<Future<Double>>();

        for (int i = 0; i < nrOfMessages; i++) {
              Callable<Double> worker = new Worker(i * nrOfElements, nrOfElements);
              Future<Double> submit = executor.submit(worker);
              list.add(submit);
        }

        for (Future<Double> future : list) {
            try {
                pi += future.get();
            } 
            catch (InterruptedException e) { e.printStackTrace(); } 
            catch (ExecutionException e) { e.printStackTrace(); }
        }
        //send results to listener
        listener.PiApproximation(pi, java.lang.System.currentTimeMillis() - start);

        executor.shutdown();
    }
}

There is a java version of this tutorial online also: http://doc.akka.io/docs/akka/2.0.1/intro/getting-started-first-java.html

However, it is NOT using Threads, it is using Actors in java. Why would you want to use Threads instad of Actors?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM