title: How synchronize in JNI?
body: <p>I'm trying to develop an android app that uses the accelerometer of the device. I want to read the first five events of the accelerometer and save their accelerations, and after that do some stuff on that.</p>
  
  <p>My JNI code is:</p>
  
  <pre><code>ASensorEventQueue* sensorEventQueue;
  ASensorManager* sensorManager;
  vector&lt;double&gt; xx, yy, zz;
  
  static int get_sensor_events(int fd, int events, void* data) {
      ASensorEvent event;
  
      while (ASensorEventQueue_getEvents(sensorEventQueue, &amp;event, 1) &gt; 0) {
          //LOGI("accelerometer x = %f y = %f z=%f t=%lld", event.acceleration.x, event.acceleration.y, event.acceleration.z, event.timestamp);
          xx.push_back(event.acceleration.x);
          yy.push_back(event.acceleration.y);
          zz.push_back(event.acceleration.z);
      }
      if (xx.size() &gt; 5) {
          ASensorManager_destroyEventQueue(sensorManager, sensorEventQueue);
          return 0;
      }
      return 1;
  }
  
  extern "C" {
  
  JNIEXPORT jint JNICALL
  Java_com_example_sensor_MainActivity_senSor(JNIEnv *env,jclass clas,jobject activity) {
      ASensorEvent event;
      int events, ident;
      ASensorRef accSensor;
  
      LOGI("sensorValue() - ALooper_forThread()");
      ALooper* looper;
      looper = ALooper_forThread();
      if(looper == NULL) looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
  
      sensorManager = ASensorManager_getInstance();
      accSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
  
      sensorEventQueue = ASensorManager_createEventQueue(sensorManager, looper, 1, get_sensor_events, NULL);
      ASensorEventQueue_enableSensor(sensorEventQueue, accSensor);
      //Sampling rate: 100Hz
      int a = ASensor_getMinDelay(accSensor);
      LOGI("min-delay: %d",a);
      ASensorEventQueue_setEventRate(sensorEventQueue, accSensor, 0);
      LOGI("sensorValue() - START");
  
      //do some stuff of vectors
      return 1;
  }
  
  }
  </code></pre>
  
  <p>The problem is the looper creates a background thread. The main thread terminates first and after that the background thread terminates, but I want to do some stuff in main thread just after the looper thread terminates.</p>