Home > Enterprise >  Accessing the same object instance from multiple Threads
Accessing the same object instance from multiple Threads

Time:06-28

I'm developing application in JavaFX and I have the following structure with the object "config" which is created in the class Controller and then I'm accessing the config from the two classes which extend the original Controller class and also from other Thread.

public class Controller {

    protected Config config = new Config();

    private void setupConfig() {
        var machineStatus = new MachineStatus();
        machineStatus.setRemainingTime(25);
        machineStatus.setMessage("Running");
        config.setMachineStatus(machineStatus);
    }
}


public class MainController extends Controller {
    Thread taskThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true) {
                        var newStatus = getNewStatus();          // Returns new status containing different RemainingTime and Message value every time
                        config.setMachineStatus(newStatus);
                        Thread.sleep(10000);
                    }
                }
    });
    taskThread.start();
}


public class StatusController extends Controller {
    System.out.println(config.getMachineStatus().getRemainingTime()) // Always prints 25
    System.out.println(config.getMachineStatus().getMessage())      // Always prints "Running"
}


public class Config {
    private MachineStatus machineStatus;

    public MachineStatus getMachineStatus() {
        return machineStatus;
    }

    public void setMachineStatus(MachineStatus value) {
        machineStatus = value;
    }
}

public class MachineStatus {
    private int remainingTime;
    private String message;

    public int getRemainingTime() {
        return remainingTime;
    }

    public void setRemainingTime(int value) {
        remainingTime = value;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String value) {
        message = value;
    }
}

I would like to achieve that when I call getRemainingTime() or get Message() in the StatusController, I will always get the latest status which was written in the config variable. But instead, I'm still getting the initial values which were stored when the variable was initialized.

The second Thread runs every 10 seconds and always updated the config variable with the new machine status. Even if I let the second Thread running for several minutes, in StatusController, I'm always getting the initial values.

Could someone help me how to achieve this?

CodePudding user response:

Ah, got it. Looks like you think that StatusController and MainController access to the same instance of Config. But it doesn't work like that.

StatusController extends Controller
MainController extends Controller

Each controller have the own Config object instance. It means that MainController change config of MainController. But StatusController have the own Config. You need to have access to the Config of MainController and print their status.

The second issue. Config is not thread safe. You may not see changes of threads due to CPU cache and etc. You could use any synchronization concept for solving it.

rzwitserloot - Already answered...

  • Related