Observable Methods and Inheritance in Java

Viewed 35

I'm fairly new to java and I've been writing a GUI that updates it's data based on sensor data received via the serial interface.

When all sensors send send data and all arraylists sync in size I change a variable to true and enable all the fields on my GUI to update with the most recent data. This check is done in a separate thread that runs on a method like so:

private void checkSensorsFilled{
    while(true){
        if (sensorFilled){// if all sensors have received some data
            this.updatePlots = true;
        } else {
            this.updatePlots = false;
        }
        try{ Thread.sleep(X); } catch (Exception e) {}
    }
}

I have a method, called from the constructor that starts this method as a separate thread like this:

private void startThreads(){
        Thread t1 = new Thread(() -> {
            this.checkSensorsFilledStatus();
        });
        t1.start();
}

The thread that consumes the updatePlots is written like this:

private void checkSensorsFilled{
    while(true){
        if (object.getFilledStatus()){// if all sensors have received some data
            updatesPlots();
        try{ Thread.sleep(X); } catch (Exception e) {}
    }
}

The problem is that the loop checking this boolean variable reads true more than once, so data is repeated on my graphs.

I know about observers and observables but I have no idea how to use them. I also know they are used as interfaces via the implements keyword, but I have no idea what this means nor how to use them in my favour in this case. Could someone explain or give examples on real applications? I find the examples online are too specific.

0 Answers
Related