title: Can Kotlin coroutines be an alternative for Java's ScheduledThreadPoolExecutor for scheduling long running background jobs?
body: <p>The traditional java approach involves getting a ScheduledThreadPoolExecutor by</p>
  
  <p><code>Executors.newScheduledThreadPool(1)</code></p>
  
  <p>And then running a bunch of <code>Runnable</code>s inside it using methods such as <code>scheduleAtFixedRate()</code>.</p>
  
  <p>However, this approach locks you into Java's threading model, which can be challenging in a Kotlin codebase where you're judiciously using Kotlin's <code>suspend</code> and made your long running jobs (e.g: Network/DB ops) in a coroutine-friendly way.</p>
  
  <p>You can always use <code>runBlocking{}</code> to forcefully run your suspend methods within the runnables to schedule them using <code>ScheduledThreadPoolExecutor</code>, but that hardly seems elegant. You're effectively going around the coroutine threading model and going back to Java's.</p>
  
  <p>So, is there a way to run long running background operations with Kotlin's coroutines? Something with an API similar to <code>scheduleAtFixedRate</code>-like methods (e.g: Run X() at 5 PM every day - a Kotlin friendly replacement of cron jobs essentially.)</p>