从多个线程访问相同的对象实例
我正在使用Javafx开发应用程序,并且具有以下结构,该结构具有在类控制器中创建的对象“ config”,然后我从两个类别访问原始控制器类以及其他线程的两个类中访问配置。
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;
}
}
当我调用getRemainingtime()或在statuscontroller中获取消息()时,我想实现这一目标,我将始终获得在配置变量中写的最新状态。但是,相反,我仍将获取初始化变量时存储的初始值。
第二个线程每10秒运行一次,并始终使用新计算机状态更新配置变量。即使我让第二个线程运行几分钟,在statuscontroller中,我也总是会得到初始值。
有人可以帮我如何实现这一目标吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,明白了。
看起来您认为STATUSCONTROLLER和MAINCONTROLLER访问配置的同一实例。但这不是那样工作的。
每个控制器都有自己的配置对象实例。这意味着MAINCONTROLLER更改MainController的配置。但是statuscontroller有自己的配置。
您需要访问Main Controller的配置并打印其状态。
第二个问题。配置不是线程安全的。您可能看不到由于CPU缓存和等。您可以使用任何同步概念来求解它。
rzwitserloot-已经回答...
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.
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...