// AvgTemp.java
public abstract class AvgTemp {
public void notifyReceived ( String eventName, Object arg) {
if (eventName.equals("temperatureMeasurement"))
{
onNewtemperatureMeasurement((TempStruct) arg);
}
}
public abstract void onNewtemperatureMeasurement(TempStruct tempStruct);
}
// MyAvgTempImpl. java
public class MyAvgTempImpl extends AvgTemp {
@Override
public void onNewtemperatureMeasurement(TempStruct tempstruct) {
//TODO : Need to write a code of calculating Average Temperature.
}
}
My question is “how to write a code of calculating average temperature (in MyAvgTempImpl. java) in situation when notification coming from many sensors ( producers of temperature data) ?”.
If that information from many sensors comes from one single thread, you will just sum them all and divide by the number of received samples.
On another hand, if that information from many sensors comes from several threads, then you will have to use:
or
or
I am not a Java programmer so I can’t tell how this is done in Java, but at least you have the general idea behind it.