title: How to synchronize coroutine?
body: <p>I am trying to make sure these two methods are synchronized. What I noticed is that Coroutines are harder to synchronize than Threads. How can I guarantee that if I call <code>start()</code> then <code>stop()</code> that my code will actually be stopped at the end? </p>
  
  <pre><code>object Test {
  
      private val coroutine = CoroutineScope(Dispatchers.IO)
  
      @Synchronized
      fun start() {
          coroutine.launch {
              // some work
          }
      }
  
      @Synchronized
      fun stop() {
          coroutine.launch {
              // clean up then stop
          }
      }
  }
  </code></pre>
  
  <p>My concern is I call <code>start()</code> then <code>stop()</code> but actually stop executes first. So my code continues when it should have stopped. </p>