This is a followup to my question here: Android thread runnable performance
I'm having some difficulty wrapping my head around synchronized methods for my app
I'm polling the sensors and storing the sensor values into arrays whenever they change
float[] accelerometerMatrix = new float[3];
float[] accelerometerWorldMatrix = new float[3];
float[] gyroscopeMatrix = new float[3];
float[] gravityMatrix = new float[3];
float[] magneticMatrix = new float[3];
float[] rotationMatrix = new float[9];
class InsertHandler implements Runnable {
public void run() {
//get values from arrays and insert into db
}
}
public void onSensorChanged(SensorEvent event) {
sensor = event.sensor;
int i = sensor.getType();
if (i == MainActivity.TYPE_ACCELEROMETER) {
accelerometerMatrix = event.values;
} else if (i == MainActivity.TYPE_GYROSCOPE) {
gyroscopeMatrix = event.values;
} else if (i == MainActivity.TYPE_GRAVITY) {
gravityMatrix = event.values;
} else if (i == MainActivity.TYPE_MAGNETIC) {
magneticMatrix = event.values;
}
long curTime = System.currentTimeMillis();
long diffTime = (curTime - lastUpdate);
// only allow one update every POLL_FREQUENCY.
if(diffTime > POLL_FREQUENCY) {
lastUpdate = curTime;
//insert into database in background thread
executor.execute(insertHandler);
}
}
Every 10ms, my app will take the current sensor values (from the arrays) and insert them into a database using a single thread executor. So the onSensorChanged
method is both writing to the arrays, and reading from the arrays to write to database
My question is, should the onSensorChanged
method be synchronized?
The most important thing is that I don't miss any data. Every 10ms I need to store the current sensor values - none can be missed.
So to my understanding, a synchronized method means that the UI thread will hold a lock, and it will write the sensor values to the arrays. During this time, the executor thread cannot read from those arrays due to the lock. Then the lock is lifted and the executor thread then locks, reads from arrays, and write to database, releases lock
I might be misunderstanding the use of synchronized methods here, especially given onSensorChanged
is event driven and I'm not sure how that plays into it
But it seems that in a situation like this, I might not be inserting the most recent values every 10ms. When the UI thread establishes a lock, the executor thread can't write those values to the database. By the time the executor thread can write, the values would now be a few ms old and inaccurate
On the other hand, synchronization would mean that I dont have situations where the UI thread is changing the array values, while at the same time the executor thread is insert half changed values into the database
So for this type of situation where I need to insert the most recent/accurate sensor data every 10ms, should I be using a synchronized method?