如何同时运行两个FOR循环 [英] How to run two FOR loops at the same time
问题描述
我正在使用加速计,第一个代码是Shake探测器:
(Code1)
I´m working with accelerometer and the first code is Shake detector: (Code1)
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
long now = System.currentTimeMillis();
//**********************************************************************
if ((now - mLastForce) > SHAKE_TIMEOUT) {
mShakeCount = 0;
}
if ((now - mLastTime) > TIME_THRESHOLD) {
long diff = now - mLastTime;
float speed = Math.abs(x + y + z - mLastX - mLastY - mLastZ) / diff * 10000;
if (speed > FORCE_THRESHOLD) {
if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
mLastShake = now;
mShakeCount = 0;
if (mShakeListener != null) {
mShakeListener.onShake();
}
}
mLastForce = now;
}
mLastTime = now;
mLastX = x;
mLastY = y;
mLastZ = z;
有了这个我收到的消息,当你的手机被震动时:
(Code2)
With this i get message, when tho phone is shaked: (Code2)
mSensorListener.setOnShakeListener(new OnShakeListener() {
@Override
public void onShake() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Shake!", Toast.LENGTH_SHORT).show();
}
});
我还有循环用于将加速度计值x,y,z每2个secons保存到数组中:
(Code3)
I have also for loop for saving accelerometer values x,y,z into array every 2 secons: (Code3)
if (lastUpdate == -1 || (curTime - lastUpdate) > 2000) {
lastUpdate = curTime;
x = values[0];
y = values[1];
z = values[2];
for (int column = 0; column < 3; column++) {
if (column == 0) {
p[row][column] = values[0];
}
if (column == 1) {
p[row][column] = values[1];
//yacc.setText("Os X: " + p[row][column]);
}
if (column == 2) {
p[row][column] = values[2];
//zacc.setText("Os X: " + p[row][column]);
}}
if (row == 0) {
xacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
}
if (row == 1) {
yacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
}
if (row == 2) {
zacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
}
if (row == 3) {
z2acc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
}
if (row == 4) {
z3acc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
}
row++;
if (row == 5) {
row = 0;
}
Code3永无止境,code1是震动检测器。我如何一起运行它,可能是线程(如何)或其他任何东西?
Code3 is never ending and code1 is shake detector. How can i run it together, maybe with threads (how) or anything else?
推荐答案
如果你想运行两个循环(或者更多)同时使用 Threads
。只需在一个线程中定义每个循环,然后启动你的主题:)
if you want to run two loops ( or more ) at the same time , use Threads
. just define each loop in one thread, and then start your threads :)
示例:
第一个帖子:
public class ThreadForLoopA extends Thread{
// variables for your Thread ...
@Override
public void run(){
// your first loop here ...
}
}
第二个帖子:
public class ThreadForLoopB extends Thread{
// variables for your Thread ...
@Override
public void run(){
// your second loop here ...
}
}
开始这样的所有主题:
ThreadForLoopA threadA = new ThreadForLoopA();
ThreadForLoopB threadB = new ThreadForLoopB();
//start threads (the two loops will be executed at the same time)
threadA.start();
threadB.start();
这篇关于如何同时运行两个FOR循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!