title: Android Service and worker thread ReentrantLock infinite loop
body: <p>Hi I want to get rid of an infinite loop which holds a worker thread in a service.</p>
  
  <p>I try to use the ReentrantLock() methods but I can't seem to get it to work.</p>
  
  <p>My worker thread calls from JNI the nativeWaitForTask method and goes to sleep.</p>
  
  <p>But my service main thread can't seem to wake him up in the submitTask method where he should signal the worker thread. </p>
  
  <p>It's like the await() call blocked the service's main thread aswell.</p>
  
  <p>But they are on different threads as far as I know... </p>
  
  <p>What did I do wrong? Why doesn't it work?</p>
  
  <pre><code>    private synchronized void nativeWaitForTask() {
  
      threadLock.lock();
  
      try {
  
  
          while(opcode == SR_Manager.STATE_IDLE) {
  
              newTask.await();
          }
  
      }catch (InterruptedException e) {
  
          e.printStackTrace();
          Log.e(SR_Manager.DEBUG_TAG,"Failed to wait for new task! " + e.toString());
  
      }finally {
  
          threadLock.unlock();
      }
  }
  
  
      private synchronized void submitTask() {
  
      if (nativeMain.isAlive()) {
  
          dstImageName = sr.getDstImageName();
  
          if (sr.getSrcImagePath() != null &amp;&amp; !sr.getSrcImagePath().equals(srcImagePath)) {
  
              srcImagePath = sr.getSrcImagePath();
              width = sr.getWidth();
              height = sr.getHeight();
              format = sr.getFormat();
              algorithm = sr.getAlgorithm();
              value = sr.getValue();
              saveOutput = sr.getSaveOutput();
              opcode = SR_Manager.STATE_LOAD_IMAGE;
  
          } else if (sr.getOpcode() != SR_Manager.STATE_LOAD_IMAGE) {
  
              algorithm = sr.getAlgorithm();
              value = sr.getValue();
              saveOutput = sr.getSaveOutput();
              opcode = sr.getOpcode();
          }
  
          t0 = System.currentTimeMillis();
  
      } else {
  
          // No native thread no service...
          silentCrash(-100);
      }
  
      // Wake the worker thread which waits for the new task condition
      threadLock.lock();
      newTask.signal();
      threadLock.unlock();
  }
  </code></pre>