我需要建立一些东西,我不知道如何。我希望有人能引导我走上正确的道路,或者告诉我如何去做。
我正在使用机器,这台机器会产生一些输出。此输出通过另一个程序读取。我正在通过我在任务中使用流程构建器创建的流程来读取此输出。需要处理此输出,并且必须更新屏幕上的多个值。它们都包含不同的消息,但消息取决于进程的输出。
(我需要读取秤的输出,它给出了产品的重量和当前时间。产品的重量,当前时间和价格需要从中减去/计算并需要显示在屏幕上。)
我无法使用观察者模式,因为屏幕将从另一个线程更新,这将触发错误。我也不能使用任务的updateMessage函数并将标签绑定到message属性,因为所有标签都有不同的输出。
我能做什么/应该做什么?你能指导我走上正确的轨道吗?
答案 0 :(得分:1)
你基本上可以像这样构建它:
Thread machineReadThread = new Thread(() -> {
boolean finished = false ;
Process process = null ;
InputStream in = null ;
try {
process = new ProcessBuilder(...).start();
in = process.getInputStream();
while (! finished) {
double weight = readWeightFromStream(in);
Instant timestamp = readTimestampFromStream(in);
Platform.runLater(() -> updateUI(weight, timestamp));
finished = checkFinished();
}
} catch (Exception exc) {
log(exc);
} finally {
if (in != null) in.close();
if (process != null) process.destroy();
}
});
machineReadThread.setDaemon(true);
machineReadThread.start();