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 8541315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:48:14+00:00 2026-06-11T11:48:14+00:00

I have created three threads in a java program. One is the main program,

  • 0

I have created three threads in a java program. One is the main program, the others are two classes that extend Thread. The main thread represent a controller for a machine. Another thread is the actuators and the third is the sensors. The controller sets variables in its own class which is read by the actuator thread. The actuator performs certain actions according to the instructions and update its own internal variables. These are in turn read by the sensor thread which reads the actuator variables (representing real world actions) and sets its own internal variables which in turn is read by the controller and we have come full circle. The controller then sets variables according to the new sensed world etc.

The actuators are in a eternal loop sleeping 100 ms in each loop.
The sensors are also in an eternal loop sleeping 20ms per loop.

The system almost works. The main loop will miss the updates from the sensor unless I add a sleep to it as well. My question is now why that is? Even sleep(0) makes the system work. I’ve placed the statement inside the performJob(Job input) while loop. How does the java main thread without a sleep call act differently than the same thread with?
Concurrency is a fairly new subject to me.

This is the code I am using:

Main:

public class Main {
    public static void main(String[] args) {
        Controller Press = new Controller();

        Press.processUnits(1);  // no reset code at the moment, only use 1 at a time
        Press.shutdownThreads();
    }
}

Controller:

import java.util.LinkedList;

public class Controller extends Thread {
    // Constants
    static final int STATE_WAITING          = 0;
    static final int STATE_MOVE_ARMS        = 1;
    static final int STATE_MOVE_PRESS       = 2;

    static final int LOADINGARM             = 2;
    static final int UNLOADINGARM           = 1;
    static final int NOARM                  = 0;        
    static final boolean EXTEND             = true;
    static final boolean RETRACT            = false;

    private enum Jobs {
        EXTENDRETRACT, ROTATE, MAGNETONOFF, PRESSLOWERRAISE
    }

    // Class variables
    private int currentState;

    // Component instructions
    private int armChoice = 0;
    private boolean bool = false;       // on, up, extend / off, down, retract

    private boolean[] rotInstr = {false, false}; // {rotate?, left true/right false}
    private boolean errorHasOccurred = false;
    private boolean pressDir = false;
    private Sensors sense = null;
    private Actuators act = null;
    private LinkedList<Job> queue = null;

    // Constructor
    public Controller() {
        act = new Actuators(0.0f, this);
        sense = new Sensors();

        act.start();
        sense.start();

        currentState = STATE_WAITING;
        queue = new LinkedList<Job>();
    }

    // Methods

    int[] getArmInstructions() {
        int extret = (bool) ? 1 : 0;
        int[] ret = {armChoice, extret};

        return ret;
    }

    boolean[] getRotation() {
        return rotInstr;
    }

    int getControllerState() {
        return currentState;
    }

    boolean getPressDirection() {
        return pressDir;
    }

    public boolean processUnits(int nAmount) {
        if (run(nAmount)) {
            System.out.println("Controller: All units have been processed successfully.");
            return true;
        } else {    // procUnits returning false means something went wrong
            System.out.println("Controller: An error has occured. The process cannot complete.");
            return false;
        }
    }

    /*
     * This is the main run routine. Here the controller analyses its internal state and its sensors
     * to determine what is happening. To control the arms and press, it sets variables, these symbolize
     * the instructions that are sent to the actuators. The actuators run in a separate thread which constantly
     * reads instructions from the controller and act accordingly. The sensors and actuators are dumb, they
     * will only do what they are told, and if they malfunction it is up to the controller to detect dangers or
     * malfunctions and either abort or correct.
     */

    private boolean run(int nUnits) {
        /*
        Here depending on internal state, different actions will take place. The process uses a queue of jobs
        to keep track of what to do, it reads a job, sets the variables and then waits until that job has finished
        according to the sensors, it will listen to all the sensors to make sure the correct sequence of events is
        taking place. The controller reads the sensor information from the sensor thread which runs on its own
        similar to the controller.

        In state waiting the controller is waiting for input, when given the thread cues up the appropriate sequence of jobs
        and changes its internal state to state move arms

        In Move arms, the controller is actively updating its variables as its jobs are performed,
        In this state the jobs are, extend, retract and rotate, pickup and drop.
        From this state it is possible to go to move press state once certain conditions apply

        In Move Press state, the controller through its variables control the press, the arms must be out of the way!
        In this state the jobs are press goes up or down. Pressing is taking place at the topmost position, middle position is for drop off
        and lower is for pickup. When the press is considered done, the state reverts to move arms,
        */

        //PSEUDO CODE:
        //This routine being called means that there are units to be processed

        //Make sure the controller is not in a blocking state, that is, it shut down previously due to errors
        //dangers or malfunctions

        //If all ok - ASSUMED SO AS COMPONENTS ARE FAULT FREE IN THIS VERSION
        if (!errorHasOccurred) {

            //retract arms
            currentState = STATE_MOVE_ARMS;
            queue.add(new Job(Jobs.EXTENDRETRACT, LOADINGARM, RETRACT));
            queue.add(new Job(Jobs.EXTENDRETRACT, UNLOADINGARM, RETRACT));
            performWork();
            //System.out.println("Jobs added");

            //while there are still units to process
            for (;nUnits != 0; nUnits--) {
                //move the press to lower position, for unloading
                currentState = STATE_MOVE_PRESS;

                //rotate to pickup area and pickup the metal, also pickup processed
                currentState = STATE_MOVE_ARMS;
                queue.add(new Job(Jobs.EXTENDRETRACT, LOADINGARM, EXTEND));
                queue.add(new Job(Jobs.EXTENDRETRACT, UNLOADINGARM, EXTEND));
                performWork();

                //retract and rotate
                queue.add(new Job(Jobs.EXTENDRETRACT, LOADINGARM, RETRACT));
                queue.add(new Job(Jobs.EXTENDRETRACT, UNLOADINGARM, RETRACT));
                performWork();

                //state change, press moves to middle position
                currentState = STATE_MOVE_PRESS;

                //state change back, put the metal on the press, drop processed and pull arms back
                currentState = STATE_MOVE_ARMS;
                queue.add(new Job(Jobs.EXTENDRETRACT, LOADINGARM, EXTEND));
                queue.add(new Job(Jobs.EXTENDRETRACT, UNLOADINGARM, EXTEND));
                queue.add(new Job(Jobs.EXTENDRETRACT, LOADINGARM, RETRACT));
                queue.add(new Job(Jobs.EXTENDRETRACT, UNLOADINGARM, RETRACT));
                performWork();

                //state change, press the metal in upper position
                currentState = STATE_MOVE_PRESS;

                //repeat until done
            }

            //unload final piece

            //move the press to lower position for unload

            //rotate and pickup processed piece

            //drop it off at unloading and wait for more orders
            currentState = STATE_WAITING;
        }

        return true;
    }


    private boolean performWork() {
        while (!queue.isEmpty()) {
                performJob(queue.removeFirst());
        }

        return true;
    }

    private boolean performJob(Job input) {
        //The purpose of this function is to update the variables and wait until they are completed

        // read in the job and check appropriate sensors
        boolean[] data = sense.getSensorData(); // LExt, LRet, UlExt, UlRet
        printBools(data);
        int Instruction = input.Instruction;
        boolean skipVars = false;

        if (input.Job == Jobs.EXTENDRETRACT) {
            if (currentState != STATE_MOVE_ARMS){
                System.out.println("Wrong state in performJob. State is "+currentState+" expected "+STATE_MOVE_ARMS);
                return false;
            }

            if ((Instruction == LOADINGARM) && (input.Bool)) skipVars = data[0];
            if ((Instruction == LOADINGARM) && (!input.Bool)) skipVars = data[1];
            if ((Instruction == UNLOADINGARM) && (input.Bool)) skipVars = data[2];
            if ((Instruction == UNLOADINGARM) && (!input.Bool)) skipVars = data[3];
        }


        if (!skipVars) {
            // if sensors not at intended values, update correct variables
            System.out.println("Controller: Did not skip vars");

            switch (input.Job) {
                case EXTENDRETRACT:
                    armChoice = (Instruction == LOADINGARM) ? LOADINGARM : UNLOADINGARM;
                    bool = input.Bool;  
                    break;

                case ROTATE:
                    break;

                case MAGNETONOFF:
                    break;

                case PRESSLOWERRAISE:
                    break;

                default: 
                    System.out.println("Default called in performJob()");
                    break;
            }
        }

        // listen to sensors until completed
        boolean done = false;


        System.out.println("Waiting for sensor data.");
        //System.out.print("Instruction is "+Instruction+" and bool is "); if (input.Bool) System.out.print("true\n"); else System.out.print("false\n");
        while (!done) {
            data = sense.getSensorData();

            // REMOVING THIS TRY STATEMENT BREAKS THE PROGRAM
            try {
                  Thread.currentThread().sleep(10);
            } catch (InterruptedException e) {
                System.out.println("Main thread couldn't sleep.");
            }

            // Check appropriate sensors
            if (input.Job == Jobs.EXTENDRETRACT) {
                if ((Instruction == LOADINGARM) && (input.Bool)) done = data[0];
                if ((Instruction == LOADINGARM) && (!input.Bool)) done = data[1];
                if ((Instruction == UNLOADINGARM) && (input.Bool)) done = data[2];
                if ((Instruction == UNLOADINGARM) && (!input.Bool)) done = data[3];

            }
        }

        // reset all variables
        armChoice = 0;
        bool = false;

        // when done return
        System.out.println("Finished "+input.Job);
        return true;
    }

    public void shutdownThreads() {
        sense.shutDown();
        act.shutDown();
    }

    private class Job {
        // Class variables
        Jobs Job;
        int Instruction;
        boolean Bool;   // used for directions, up/down, left/right, extend/retract

        // Constructor
        Job(Jobs newJob, int newInstruction, boolean dir) {
            Job = newJob;
            Instruction = newInstruction;
            Bool = dir;
        }
    }

    private void printBools(boolean[] input) {
        System.out.println();
        for (int i = 0; i < input.length; i++) {
            if (input[i]) System.out.print("true "); else System.out.print("false "); 
        }
        System.out.println();
    }
}

Actuators:

public class Actuators extends Thread {
    // Constants
    private final int ARM = 0, ROTATE = 0; // array indexes - which arm, rotate yes/no? 
    private final int DIR = 1, ROTDIR = 1; // array indexes - which direction ext/ret, rotate direction
    private final int EXT = 1;//, RET = 0;
    private final double ARM_SPEED = 5.0;

    // Class variables
    private Controller Owner = null;
    private boolean run = true;

    // Constructor
    Actuators(float nPos, Controller Owner) {
       Reality.changeAngle(nPos);
       this.Owner = Owner;
    }

    // Methods
    private void rotate(boolean dir) {
        float nAngle = dir ? 0.1f : -0.1f;
        Reality.changeAngle(nAngle);
    }

    public void run() {
        while (run) {
            try {
                sleep(100);
            } catch (InterruptedException e) {
                System.out.println("Actuators couldn't sleep");
            }

            // read variables in controller
            int nState = Owner.getControllerState();

            if (nState == Controller.STATE_MOVE_ARMS) {
                boolean[] rot = Owner.getRotation();

                if (rot[ROTATE]) { // Rotation?
                    rotate(rot[ROTDIR]); 
                } else { // or arm extensions
                    int[] instr = Owner.getArmInstructions();

                    if (instr[ARM] != Controller.NOARM) {   // 0 = no arm movement
                        //System.out.println("Actuator arm is "+instr[ARM]);
                        double dir = (instr[DIR] == EXT) ? ARM_SPEED : -ARM_SPEED;  // 1 = extend, 0 = retract
                        Reality.changeArmLength(instr[ARM], dir);
                    }
                }               
            }
        }
    }

    void shutDown() {
        run = false;
    }
}

Reality is a class composed of static fields and methods, written to by the actuators and read by sensors.

public class Reality {
    // Constants
    static private final double EXTEND_LIMIT = 100.0;
    static private final double RETRACT_LIMIT = 0.0;

    // Variables
    private static float ArmsAngle = 0.0f;

    // Read by Sensor
    static double LoadingArmPos = 0.0;
    static double UnloadingArmPos = 0.0;

    // Methods

    static void changeAngle(float newAngle) {
        ArmsAngle = ArmsAngle + newAngle;

        if ((ArmsAngle < 0.0f) || (ArmsAngle > 90.0f))
            System.out.println("Reality: Unallowed Angle");
    }

    static void changeArmLength(int nArm, double dPos) {    // true = extend, false = retract
        switch (nArm) {
            case Controller.LOADINGARM:
                LoadingArmPos += dPos;
                checkArmPos(LoadingArmPos);
                break;

            case Controller.UNLOADINGARM:
                UnloadingArmPos += dPos;
                checkArmPos(UnloadingArmPos);
                break;

            default:
                System.out.println("Arm other than 2 (load) or 1 (unload) in changeArmLength in Reality");
                break;
        }
    }

    static float senseAngle() {
        return ArmsAngle;
    }

    static private boolean checkArmPos(double dPos) {
        // Allowed positions are 100.0 to 0.0
        if ((dPos > EXTEND_LIMIT) || (dPos < RETRACT_LIMIT)) {  
            System.out.println("Arm position impossible in reality. Is "+dPos);
            return true;
        } else {
            return false;
        }
    }
}

Finally the sensors:

public class Sensors extends Thread {
    // Constants
    private final double EXTENDED = 100.0;
    private final double RETRACTED = 0.0;
    private final double MARGIN = 0.1;

    // Class Variables
    private boolean run = true;

    // Read by Controller
    private boolean LoadingExtended = true;
    private boolean LoadingRetracted = true;
    private boolean UnloadingExtended = true;
    private boolean UnloadingRetracted = true;

    // Constructor
    Sensors() {         
        LoadingExtended = false;
        LoadingRetracted = true;
        UnloadingExtended = false;
        UnloadingRetracted = true;
    }


    // Methods
    boolean senseLoadingExtended() {
        return (Math.abs(Reality.LoadingArmPos - EXTENDED) < MARGIN);
    }

    boolean senseLoadingRetracted() {
        return (Math.abs(Reality.LoadingArmPos - RETRACTED) < MARGIN);
    }

    boolean senseUnloadingExtended() {
        return (Math.abs(Reality.UnloadingArmPos - EXTENDED) < MARGIN);
    }

    boolean senseUnloadingRetracted() {
        return (Math.abs(Reality.UnloadingArmPos - RETRACTED) < MARGIN);
    }

    // called by Controller
    boolean[] getSensorData() {
        boolean[] ret = {LoadingExtended, LoadingRetracted, UnloadingExtended, UnloadingRetracted};
        return ret;
    }

    // Sensor primary loop
    public void run() {
        while (run) {
            try {
                sleep(20);
            }
            catch (InterruptedException e) {
                System.out.println("Sensors couldn't sleep");
            }

            LoadingExtended = senseLoadingExtended();
            LoadingRetracted = senseLoadingRetracted();
            UnloadingExtended = senseUnloadingExtended();
            UnloadingRetracted = senseUnloadingRetracted();
        }
    }

    void shutDown() {
        run = false;
    }

}

Not all fields and functions are read in this version. The program is a reworking of a previous single thread application mostly using function calls. I’ve cleaned up the code a bit for readability. Constructive design remarks are welcome even though it was not the original question. There is something really fishy going on. I am usually not a superstitious coder but I can for example replace the sleep call with a System.out.println() call and the program will work.

  • 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-11T11:48:15+00:00Added an answer on June 11, 2026 at 11:48 am

    What occurred here was most probably a Memory Consistency Error. When the controller class set the internal control variables and then entered the loop waiting for sensors it most likely prevented the Actuators and Sensors classes from properly updating the readings seen to the controller and as such prevented the controller from seeing the correct values. By adding the synchronize statement to all functions which read from another class the problem was solved. I can only speculate that the sleep call had the controller thread enter a synchronized block of some kind which let the other threads’ changes to the variables become visible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a div there are two childs of that div one is
i create three threads in my main program. i have for loop in each
I have a Java program that uses reflection to find and invoke the main(String[]
i have created three linear layouts of equal weights. Inside one of the linear
i am new to iPad developer, i have created two or three iPad application
i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created
I have two classes; one is a server, another one is a client, and
I have a Java program that needs to call the same external executable 6
I have created a Java Applet that will be used in IE to print
I have a simple program with one TextView and two Buttons: Button1 and Button2.

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.