【问题标题】:How can I not use a global variable in this embedded program?如何在这个嵌入式程序中不使用全局变量?
【发布时间】:2020-04-16 19:26:23
【问题描述】:

我相信我们公司有一个边缘案例,它阻止我进入“从不使用全局变量阵营”。

我们需要编写一个在我们的盒子中运行的嵌入式应用程序,它可以从医院的设备中提取医疗数据。

即使医疗设备被切断、网络中断或我们的盒子设置发生变化,它也应该无限运行。设置是从 .txt 文件中读取的,可以在运行时更改,最好没有问题。

这就是为什么单例模式对我没用。所以我们不时返回(读取 1000 条数据后)并读取设置,如下所示:

public static SettingForIncubator settings;

public static void main(String[] args) {
    while(true){
        settings = getSettings(args);

        int counter=0;

        while(medicalDeviceIsGivingData && counter < 1000){
            byte[] data = readData(); //using settings

            //a lot of of other functions that use settings.

            counter++;
        }
        Thread.Sleep(100); //against overheating and CPU usage.
    } 
    public byte[] readData(){
    //read Data from the port described in settings variable
    }
}

还有其他方法可以设计程序吗?

【问题讨论】:

  • 此代码未在循环内使用全局settings
  • while(true) 没有 Thread.sleep 会让你的 CPU 很快达到 100%
  • 我当然使用 Thread.sleep()。我使用 readData() 中的设置。我会在伪代码中添加它们。
  • @OnatKorucu 您已将“设置”定义为类变量,然后在主函数中再次定义,当在 readData() 中使用一个时?
  • @BHAWANI SINGH 你是对的,在伪代码中我不小心定义了两次设置。我正在修复它。

标签: java while-loop global-variables global static-variables


【解决方案1】:

我不知道你的意思,但我认为这段代码会对你有所帮助:

class SettingsForIncubator {
}

public class MedicalProcessor {

    protected volatile boolean active;
    protected volatile boolean medicalDeviceIsGivingData;

    public void start(String[] args) throws Exception {
        this.active = true;
        Thread thread = new Thread(() -> loop(args));
        thread.start();
    }

    protected void loop(String[] args) {
        System.out.println("start");
        while(active) {
            SettingsForIncubator settings = getSettings(args);
            int counter=0;

            while(medicalDeviceIsGivingData && counter < 1000){
                byte[] data = readData(settings); //using settings

                //a lot of of other functions that use settings.

                counter++;
            }
            try {
                Thread.sleep(100); //against overheating and CPU usage.
            }
            catch (Exception e) {
                break;
            }
        }
    }

    protected byte[] readData(SettingsForIncubator settings) {
        // logic read data
        return null;
    }

    protected SettingsForIncubator getSettings(String[] args) {
        // logic to get settings
        return null;
    }

    public void stop() {
        this.active = false;
    }

    public static void main(String[] args) throws Exception {
        MedicalProcessor processor = new MedicalProcessor();
        processor.start(args);
    }

}

【讨论】:

  • 谢谢,但是循环函数和我的主函数使用相同的固有结构。虽然封装全局变量乍一看限制了它的范围,但实际上我的 main 函数中的所有函数都可能需要设置。所以可能我需要把我所有的 main(args) 放在 loop(args) 中。
  • 作为另一个问题,这种架构有什么好处?我认为这很好,因为它基本上意味着 loop() 之外的代码将看不到“设置”变量。
  • 这个架构没有阻塞主线程,你可以在主线程上做任何事情,并且这个架构可以让你的程序在你需要的时候通过调用'stop'函数停止
猜你喜欢
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2015-05-11
  • 1970-01-01
  • 2018-05-09
相关资源
最近更新 更多