Search code examples
androidthread-safetyandroid-sensors

Is synchronisation needed in onSensorChanged(...)?


I got a SensorEventListener which does in the onSensorChanged() method something like the following:

public void onSensorChanged(SensorEvent e){
   values = e.values.clone();
   handleEvent(); //do some computations
}

Where values is a class variable which will be accessed in the handleEvent() method in order to do some computations. My question is now whether I have to lock the variable values, i.e. could values be overridden by another thread calling onSensorChanged() while some other thread is in handleEvent()?

thanks in advance for any help!


Solution

  • OnSensorChanged gets called inside the UI-Thread so if you access values in another thread than the ui-thread you need to synchronize or use other techniques like a queues for example.