Note that there are some explanatory texts on larger screens.

plurals
  1. POAkka actor tutorial in Scala to multithreading in Java
    primarykey
    data
    text
    <p>I tried to convert the Getting-started Akka tutorial from Scala to Java and replace actors with threads.</p> <p>The tutorial can be found here <a href="http://typesafe.com/resources/tutorials/getting-started-with-akka-scala.html" rel="nofollow">http://typesafe.com/resources/tutorials/getting-started-with-akka-scala.html</a></p> <p>The original Pi.scala file can be checked out from Typesafe giter8 template repository </p> <p><code>g8 typesafehub/akka-first-tutorial-scala</code></p> <p>I would like to know if what I did is correct.</p> <p>And this is my Pi.java</p> <pre><code>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&lt;Double&gt; { 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 &lt; 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&lt;Future&lt;Double&gt;&gt; list = new ArrayList&lt;Future&lt;Double&gt;&gt;(); for (int i = 0; i &lt; nrOfMessages; i++) { Callable&lt;Double&gt; worker = new Worker(i * nrOfElements, nrOfElements); Future&lt;Double&gt; submit = executor.submit(worker); list.add(submit); } for (Future&lt;Double&gt; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload