title: Can I take advantage of Kotlin's Coroutines by using them in Java code?
body: <p><strong>What's my goal?</strong></p>
  
  <p>My goal is to be able to use Kotlin's Coroutine system from Java. I want to be able to pause mid-execution for a given amount of time, and then pick back up at that spot after the given amount of time has passed. From Java, I'd like to be able to execute tasks that allow pausing mid-execution without in an asynchronous fashion, such as:</p>
  
  <pre><code>//example 1
  someLogic();
  pause(3000L); //3 seconds
  someMoreLogic();
  
  //example 2
  while(true) {
      someContinuedLogic();
      pause(10000L); //10 seconds
  }
  </code></pre>
  
  <p><strong>What's my issue?</strong></p>
  
  <p>As expected, I am able to execute coroutines perfectly fine from Kotlin, but when it comes to Java, it becomes tricky because the Java part of the code executes the entire block at once without any pauses, whereas the Kotlin block correctly pauses 1, and then 4 seconds.</p>
  
  <p><strong>What's my question?</strong></p>
  
  <p>Is it even possible to use Kotlin as a backbone for coroutines in Java? If so, what am I doing wrong? <em>Below you can find the source code showing how I am attempting to use Kotlin's coroutines in Java.</em></p>
  
  <p><strong>KtScript Class</strong></p>
  
  <pre><code>abstract class KtScript {
  
      abstract fun execute()
  
      fun &lt;T&gt; async(block: suspend () -&gt; T): CompletableFuture&lt;T&gt; {
          val future = CompletableFuture&lt;T&gt;()
          block.startCoroutine(completion = object : Continuation&lt;T&gt; {
              override fun resume(value: T) {
                  future.complete(value)
              }
              override fun resumeWithException(exception: Throwable) {
                  future.completeExceptionally(exception)
              }
          })
          return future
      }
  
      suspend fun &lt;T&gt; await(f: CompletableFuture&lt;T&gt;): T =
              suspendCoroutine { c: Continuation&lt;T&gt; -&gt;
                  f.whenComplete { result, exception -&gt;
                      if (exception == null)
                          c.resume(result)
                      else
                          c.resumeWithException(exception)
                  }
              }
  
      fun pause(ms: Long): CompletableFuture&lt;*&gt; {
          //todo - a better pausing system (this is just temporary!)
          return CompletableFuture.runAsync {
              val currentMs = System.currentTimeMillis()
              while (System.currentTimeMillis() - currentMs &lt; ms) {
                  /* do nothing */
              }
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Kotlin Execution Code</strong></p>
  
  <pre><code>fun main(args: Array&lt;String&gt;) {
      ScriptTestKotlin().execute()
  }
  
  class ScriptTestKotlin : KtScript() {
      override fun execute() {
          println("Executing Kotlin script from Kotlin...")
          val future = async {
              await(pause(1000L))
              println("   1 second passed...")
              await(pause(4000L))
              println("   5 seconds passed...")
          }
          future.get() //wait for asynchronous task to finish
          println("Finished!")
      }
  }
  </code></pre>
  
  <p><strong>Kotlin Execution Results</strong></p>
  
  <pre><code>Executing Kotlin script from Kotlin...
     1 second passed...
     5 seconds passed...
  Finished!
  </code></pre>
  
  <p><strong>Java Execution Code</strong></p>
  
  <pre><code>public class ScriptTestJava extends KtScript {
  
      public static void main(String[] args) {
          new ScriptTestJava().execute();
      }
  
      @Override
      public void execute() {
          System.out.println("Executing Kotlin script from Java...");
          CompletableFuture&lt;?&gt; future = async(continuation -&gt; {
              await(pause(1000L), continuation);
              System.out.println("    1 second passed...");
              await(pause(4000L), continuation);
              System.out.println("    5 seconds passed...");
              return continuation;
          });
          try {
              future.get(); //wait for asynchronous task to finish
          } catch(Exception e) {
              e.printStackTrace();
          }
          System.out.println("Finished!");
      }
  }
  </code></pre>
  
  <p><strong>Java Execution Results</strong></p>
  
  <pre><code>Executing Kotlin script from Java...
      1 second passed...
      5 seconds passed...
  Finished!
  </code></pre>
  
  <p>^^^ Unfortunately, the pauses are skipped in Java. ^^^</p>