我的应用程序正在以很高的速度进行繁重的算法计算。每次更改方向传感器时,如果尚未运行,它将激活算法。该算法是Threaded
因为无论算法是否运行,我都希望继续使用我的应用程序Buttons
和Exit
按钮。如果我method
在trackingActivity
“算法”上调用“算法” ,则该算法已被激活,但是我无法button
在我的应用程序屏幕上使用任何算法。在这种情况下,线程化很有用,尤其是因为我需要在应用程序主屏幕上显示算法结果。这就是为什么我需要使用Threading
-使算法成为并行过程。我的主要建议是使用.join()
这是因为只要当前的算法执行已在运行,我就不希望传感器更改中断来激活我的算法。
那是我的代码&Thread
:
package com.application.i;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class trackingActivity extends Activity implements SensorEventListener {
TextView Xlocation;
TextView Ylocation;
TextView Zlocation;
TextView Iterations;
TextView Pit;
TextView pitch;
TextView roll;
TextView yaw;
private double azimuth_angle;
private double roll_angle;
private double pitch_angle;
float[] orientations;
private SensorManager mSensorManager;
private Sensor mOrientation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackingsub);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mOrientation,
SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
trackingActivity t = new trackingActivity();
double[] Locations = t.Algo(pitch_angle, roll_angle, azimuth_angle);
}
public void onSensorChanged(SensorEvent event) {
azimuth_angle = event.values[0];
pitch_angle = event.values[1];
roll_angle = event.values[2];
// Do something with these orientation angles.
double[] orientations = { pitch_angle, roll_angle, azimuth_angle };
// mSensorManager.unregisterListener(this);
pitch = (TextView) findViewById(R.id.pitcher);
roll = (TextView) findViewById(R.id.roller);
yaw = (TextView) findViewById(R.id.yawer);
String p = String.valueOf(pitch_angle);
String r = String.valueOf(roll_angle);
String a = String.valueOf(azimuth_angle);
pitch.setText(p);
roll.setText(r);
yaw.setText(a);
while(true){
new Thread(new Runnable() {
public void run()
{
try
{
synchronized (this) {
X = 10.6569;
Y = 20.3265;
Z = 30.259;
locations = algo(X,Y,Z,pitch_angle, roll_angle,
azimuth_angle);
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
我注意到该algo
方法没有顺序执行,我的意思是这Thread
不要等待其先前的操作并开始new Thread
。
通过在线搜索,我发现.join()
,.join()
强制aThread
等待直到完成,然后在这种情况下才重新开始。
我的问题是:如何强制此Thread
操作仅在其先前的激活完成后才开始?或者,如何在.join()
这里实现呢?
解决方案:
while(true){
Thread t = new Thread(new Runnable() {
public void run()
{
try
{
synchronized (this) {
X = (double) Float.parseFloat(Splited2[textIndex]);
Y = (double) Float.parseFloat(Splited2[textIndex+1]);
Z = (double) Float.parseFloat(Splited2[textIndex+2]);
textIndex+=3;
locations = algo(X,Y,Z,pitch_angle, roll_angle,
azimuth_angle);
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
t.join(); \\ auto-correction my force you to use try-catch
}
假设您已经为Thread创建了一个类,例如
Thread myThread = new Thread(new Runnable(...)); myThread.start();
您可以通过将该行添加myThread.join()
为yout while循环的最后一行来等待该线程完成。
这将导致主线程在myThread继续执行之前等待myThread完成。
因此,您的代码如下所示:
while(true){
Thread myThread = new Thread(new Runnable() {
public void run()
{
try
{
synchronized (this) {
X = (double) Float.parseFloat(Splited2[textIndex]);
Y = (double) Float.parseFloat(Splited2[textIndex+1]);
Z = (double) Float.parseFloat(Splited2[textIndex+2]);
textIndex+=3;
locations = algo(X,Y,Z,pitch_angle, roll_angle,
azimuth_angle);
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
myThread.start();
myThread.join();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句