How to run two FOR loops at the same time

I work with an accelerometer, and the first code is with a 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; 

With this, I get a message when the phone shakes: (Code2)

  mSensorListener.setOnShakeListener(new OnShakeListener() { @Override public void onShake() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Shake!", Toast.LENGTH_SHORT).show(); } }); 

I also have a loop to save the values โ€‹โ€‹of the accelerometer x, y, z to the array every 2 seconds: (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 never ends, and code1 with a jitter detector. How can I run it together, maybe with threads (like) or something else?

+4
source share
1 answer

if you want to run two loops (or more) at the same time, use Threads . just define each loop in one thread and then run your threads :)

Example:

The first topic:

 public class ThreadForLoopA extends Thread{ // variables for your Thread ... @Override public void run(){ // your first loop here ... } } 

Second thread:

 public class ThreadForLoopB extends Thread{ // variables for your Thread ... @Override public void run(){ // your second loop here ... } } 

Start all of your topics as follows:

 ThreadForLoopA threadA = new ThreadForLoopA(); ThreadForLoopB threadB = new ThreadForLoopB(); //start threads (the two loops will be executed at the same time) threadA.start(); threadB.start(); 
+8
source

Source: https://habr.com/ru/post/1384370/


All Articles