title: How to lock thread correctly with ReentrantLock or Mutex?
body: <p>I have an acync method with callback:</p>
  
  <pre><code>myAsyncMethod(params) {
      handleResult(it)
  }
  </code></pre>
  
  <p>I need to convert it to sync method (block and return result) like this:</p>
  
  <pre><code>val result = mySyncMethod(params)
  handleResult(result)
  </code></pre>
  
  <p>I can't re-implement it by another way because this method provided by third-party library.</p>
  
  <p>I am trying to use ReentrantLock:</p>
  
  <pre><code>fun mySyncMethod(params:Type) {
      Log.d("tag", "1")
      val result = null
      val mutex = ReentrantLock()
      myAsyncMethod(params) {
          Log.d("tag", "3")
          result = it
          mutex.unlock()
      }
      Log.d("tag", "2")
      mutex.lock()
      Log.d("tag", "4")
      return result
  }
  
  handleResult(mySyncMethod(params))
  </code></pre>
  
  <p>I wait that I should see 1, 2, 3, 4. But I get 1, 2, 4, 3 and null in handleResult. I am trying the same with mutex but with the same result. How to make it works?</p>
  
  <p>P.S. Sure I can use synchronized, but in this case I have to use Object-variable additionally.</p>