Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8368967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:31:24+00:00 2026-06-09T13:31:24+00:00

There is a bug somewhere in the code below (trying recursion with Akka). Algorithm

  • 0

There is a bug somewhere in the code below (trying recursion with Akka). Algorithm stops and the process (Java Application) is executed forever in JVM unless I kill it from the System Monitor. I believe it should be a very simple hack to fix it.

Here is an example on how to use Akka for parallel Pi approximation. Below is an attempt to show how Akka works with recursive Actors. So the master creates 2 workers, sends them the same message to decrement some int value. They do that in parallel and check if the integer value is not equal to 0. If so, they return the result integer value (0) to the master or they both create again 2 workers and send them a recently decremented value.. If the depth of this tree is greater than 1 (the integer was of >1 value) then workers send their results to the workers that called them and only in the end to the master. Well, it is really easy as below (Decrement, NewIntValue and FinalIntValue are essentially the same, they have different names to make it more understandable):

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;
import akka.routing.RoundRobinRouter;

public class StackOverFlow {

    public static void main(String[] args) {
        StackOverFlow rid = new StackOverFlow();
        rid.start(2);
    }

    public void start(final int workersNumber) {
        // create an Akka system
        ActorSystem system = ActorSystem.create("IntDec");
        // create the result listener, which will print the result and shutdown the system
        final ActorRef listener = system.actorOf(new Props(Listener.class), "listener");
        // create the master
        ActorRef master = system.actorOf(new Props(new UntypedActorFactory() {
            public UntypedActor create() {
                return new Master(workersNumber, listener);
            }
        }), "master");
        // start the computation
        master.tell(new Compute());
    }

    static class Compute {}

    static class Decrement {
        private final int intValue;
        public Decrement(int value) {
            this.intValue = value;
        }
        public int getValue() {
            return intValue;
        }
    }

    static class NewIntValue {
        private final int intValue;
        public NewIntValue(int value) {
            intValue = value;
        }
        public int getValue() {
            return intValue;
        }
    }

    static class FinalIntValue {
        private final int intValue;
        public FinalIntValue(int value) {
            intValue = value;
        }
        public int getValue() {
            return intValue;
        }
    }

    public static class Worker extends UntypedActor {

        private int resultsNumber = 0;
        private final int messagesNumber = 2;

        private final ActorRef workerRouter;

        public Worker(final int workersNumber) {

            workerRouter = getContext().actorOf(
                    new Props(new UntypedActorFactory() {
                        public UntypedActor create() {
                            return new Worker(workersNumber);
                        }
                    }).withRouter(
                        new RoundRobinRouter(workersNumber)
                    ), "workerRouter");

        }

        public void onReceive(Object message) {

            if (message instanceof Decrement) {
                // get and decrement the int value
                Decrement job = (Decrement) message;
                int intValue = job.getValue();
                System.out.println("\tWorker:Decrement " + intValue);
                intValue--;
                if (intValue == 0) {
                    // we are finished
                    getSender().tell(new NewIntValue(intValue), getSelf());
                    // stop this actor and all its supervised children
                    getContext().stop(getSelf());
                } else {
                    for (int i = 0; i < messagesNumber; i++) {
                        // notify a worker
                        workerRouter.tell(new Decrement(intValue), getSelf());
                    }
                }

            } else if (message instanceof NewIntValue) {

                NewIntValue newInt = (NewIntValue) message;
                int intValue = newInt.getValue();

                System.out.println("\tWorker:NewIntValue!!! " + intValue);

                resultsNumber++;
                if (resultsNumber == messagesNumber) {
                    // we are finished
                    getSender().tell(new NewIntValue(intValue), getSelf());
                    // stop this actor and all its supervised children
                    getContext().stop(getSelf());
                }

            } else unhandled(message);
        }

    }

    public static class Master extends UntypedActor {

        private int resultsNumber = 0;
        private final int messagesNumber = 2;

        private int intValue = 2;

        private final ActorRef listener;
        private final ActorRef workerRouter;

        public Master(final int workersNumber, ActorRef listener) {

            this.listener = listener;

            workerRouter = getContext().actorOf(
                    new Props(new UntypedActorFactory() {
                        public UntypedActor create() {
                            return new Worker(workersNumber);
                        }
                    }).withRouter(
                        new RoundRobinRouter(workersNumber)
                    ), "workerRouter");

        }

        public void onReceive(Object message) {

            if (message instanceof Compute) {

                System.out.println("\tMaster:Compute " + intValue);

                System.out.println(
                        "\n\tInitial integer value: " + intValue);

                for (int i = 0; i < messagesNumber; i++) {
                    workerRouter.tell(new Decrement(intValue), getSelf());
                }

            } else if (message instanceof NewIntValue) {

                NewIntValue newInt = (NewIntValue) message;
                intValue = newInt.getValue();

                System.out.println("\tMaster:NewIntValue " + intValue);

                resultsNumber++;
                if (resultsNumber == messagesNumber) {
                    // send the result to the listener
                    listener.tell(new FinalIntValue(intValue), getSelf());
                    // stop this actor and all its supervised children
                    getContext().stop(getSelf());
                }

            } else unhandled(message);

        }

    }

    public static class Listener extends UntypedActor {

        public void onReceive(Object message) {

            if (message instanceof FinalIntValue) {
                FinalIntValue finalInt = (FinalIntValue) message;
                System.out.println(
                        "\n\tFinal integer value: " + finalInt.getValue());
                getContext().system().shutdown();
            } else {
                unhandled(message);
            }

        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:31:26+00:00Added an answer on June 9, 2026 at 1:31 pm
    1. add private ActorRef sender; to the Worker class;
    2. add sender = getSender(); at the beginning of the Decrement message;
    3. change getSender() to sender in the NewIntValue method of the
      Worker class;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes some questions get close :) There is a Bug in Java ( been
Ok guys, could you tell me is there a certain bug in current code:
Somewhere in our Ruby code, there are three lines that (currently, for debugging purposes)
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
There's a bug in my app which shows up with the following (partial) stacktrace:
It seems to be well-known there is a bug when using JMenuItem.getRootPane(). I read
I have developed an AJAX based game where there is a bug caused (very
i am using icefaces 3, and there's a bug in some components in binding
While using fixed width select tag , there is one bug in IE. When
I'm using Zend_File_Transfer_Adapter_Http to upload file to my server. There is a bug when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.