title: Correctly implementing wait and notify in Kotlin
body: <p>According to this document, using <code>wait</code> and <code>notify</code> is discouraged in Kotlin: <a href="https://kotlinlang.org/docs/reference/java-interop.html" rel="noreferrer">https://kotlinlang.org/docs/reference/java-interop.html</a></p>
  
  <blockquote>
    <p>wait()/notify()</p>
    
    <p>Effective Java Item 69 kindly suggests to prefer concurrency utilities to wait() and notify(). Thus, these methods are not available on references of type Any. </p>
  </blockquote>
  
  <p>However the document does not propose any correct way of doing it.</p>
  
  <p>Basically, I would like to implement a service, which would read the input data and process them. If there were no input data, it would suspend itself until someone notifies that there are new input data. Something like</p>
  
  <pre><code>while (true) {
      val data = fetchData()
      processData(data)
      if (data.isEmpty()) {
          wait()
      }
  }
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>I don't want to use these not recommended methods (antipatterns), I really want to find out how to do this properly.</p>
  
  <p>In my case <code>fetchData</code> reads data from the database, so queues in my case cannot be used.</p>