Sim*_*mon 5 java multithreading android synchronization android-sensors
这是我的问题的后续跟进:Android线程可运行的性能
我在应用程序的同步方法上遇到了一些困难
我正在轮询传感器并在传感器值发生变化时将其存储到数组中
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);
}
}
Run Code Online (Sandbox Code Playgroud)
每隔10毫秒,我的应用程序将获取当前传感器值(来自数组)并使用单个线程执行程序将它们插入到数据库中.因此,该onSensorChanged方法既可以写入数组,也可以从数组中读取数据
我的问题是,该onSensorChanged方法应该同步吗?
最重要的是我不会错过任何数据.每隔10ms我需要存储当前的传感器值 - 没有一个可以错过.
所以根据我的理解,同步方法意味着UI线程将保持锁定,并将传感器值写入数组.在此期间,执行程序线程无法从锁定中读取这些数组.然后解除锁定,然后执行程序线程锁定,从数组读取,并写入数据库,释放锁定
我可能在这里误解了同步方法的使用,特别是考虑到onSensorChanged事件驱动,我不确定它是如何发挥作用的
但似乎在这种情况下,我可能不会每隔10ms插入最新的值.当UI线程建立锁定时,执行程序线程无法将这些值写入数据库.到执行程序线程可以写入时,这些值现在将是几毫秒并且不准确
另一方面,同步意味着我没有UI线程正在更改数组值的情况,而同时执行程序线程将一半更改的值插入到数据库中
因此,对于需要每隔10ms插入最新/准确传感器数据的这种情况,我应该使用同步方法吗?
您当前的代码不是线程安全的,因为它Runnable使用 UI 线程正在写入的相同数组。一旦调用,executor.execute(insertHandler);就不能保证 UI 线程不会收到另一个传感器事件并在将数组值写入Runnable数据库之前更改其中一个值。看来你明白这部分了。
为了解决这个问题,我根本不建议使用同步块,因为您似乎只想写出存储在数组中的任何值diffTime > POLL_FREQUENCY。该onSensorChanged(...)方法本身只会在代码中的 UI 线程上调用,因此您不必担心另一个线程在此方法中更改数组的值。
综上所述,您可以做的就是将数组的当前值存储在类的新实例中Runnable。我知道您在上一篇文章中建议使用相同的实例,但这不会产生明显的差异。您甚至可以通过打开 Android Monitor 进行验证,并在应用程序运行时检查内存使用情况。onSensorChanged()通过存储当前值,现在在写出数据之前是否再次调用都无关紧要,因为您已经拥有了所需数据的副本,该副本不会更改。
这是我在代码中建议的内容:
class InsertHandler implements Runnable {
final float[] accelerometerMatrix;
final float[] accelerometerWorldMatrix;
final float[] gyroscopeMatrix;
final float[] gravityMatrix;
final float[] magneticMatrix;
final float[] rotationMatrix;
public InsertHandler(float[] accelerometerMatrix, float[] accelerometerWorldMatrix,
float[] gyroscopeMatrix, float[] gravityMatrix,
float[] magneticMatrix, float[] rotationMatrix) {
this.accelerometerMatrix = accelerometerMatrix;
this.accelerometerWorldMatrix = accelerometerWorldMatrix;
this.gyroscopeMatrix = gyroscopeMatrix;
this.gravityMatrix = gravityMatrix;
this.magneticMatrix = magneticMatrix;
this.rotationMatrix = rotationMatrix;
}
public void run() {
// use class field arrays values and insert into db
}
}
Run Code Online (Sandbox Code Playgroud)
然后当你将 the 添加Runnable到executoruse 中时:
Runnable insertHandler = new InsertHandler(accelerometerMatrix, accelerometerWorldMatrix,
gyroscopeMatrix, gravityMatrix, magneticMatrix, rotationMatrix);
executor.execute(insertHandler);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
913 次 |
| 最近记录: |