title: How to work with listener in synchronous manner?
body: <p><em>If I am not mistaken...</em></p>
  
  <p>If you launch listener your code continues running, and when the event occurs it triggers listener. So it is asynchronous.</p>
  
  <p>What I would like to do is wrap that entire code in such way, that from external code it would look like synchronous call.</p>
  
  <p><strong>Before (pseudo-code)</strong></p>
  
  <pre><code>listener:
     oh, we just read temperature value
  
  listener launch:
     register listener for temperature
     continue running
     ... 
     // somewhere here the temperature is read so we can obtain the value
     ...
  </code></pre>
  
  <p><strong>After</strong></p>
  
  <p>Additional wrapper of previous code:</p>
  
  <pre><code>while (true)
    Thread.sleep(1000)
    if (is_temperature_read)
      break;
  </code></pre>
  
  <p>and call of it:</p>
  
  <pre><code>temp = read_temperature()
  </code></pre>
  
  <p>To my surprise the loop runs infinitely and the temperature is never read (i.e. the listener is never triggered). Without wrapping it with such loop, it is triggered.</p>
  
  <p>So how to do the correct wrapping? </p>
  
  <h2>Update 1/2: real code</h2>
  
  <p>Now, this is based on Sherif elKhatib (all errors are mine).</p>
  
  <p>main code:</p>
  
  <pre><code>    final Object lock = new Object();
  </code></pre>
  
  <p>part of listener:</p>
  
  <pre><code>        @Override
          public void onSensorChanged(SensorEvent event) 
          {
              synchronized (lock) 
              {
                  read_value = event.values[0];
                  sensor_manager.unregisterListener(this,proximity_sensor);
                  lock.notifyAll();
              }
          }
  </code></pre>
  
  <p>wait for listener:</p>
  
  <pre><code>          synchronized (lock)
            {
                try
                {
                    lock.wait();
                }
                catch (InterruptedException ex)
                {
                }
            }
  </code></pre>
  
  <h2>Update 2/2: calling from another thread</h2>
  
  <p>This is not solution, just a remark. If I call this synchronous wrapper from ad-hoc created new Thread it does not fall into deadlock.</p>
  
  <p>So probably something else locks above code.</p>