1 /*
<lambda>null2  * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package examples
6 
7 import kotlinx.coroutines.*
8 import kotlinx.coroutines.swing.*
9 import java.text.*
10 import java.util.*
11 import java.util.concurrent.*
12 import kotlin.coroutines.*
13 
14 fun log(msg: String) = println("${SimpleDateFormat("yyyyMMdd-HHmmss.sss").format(Date())} [${Thread.currentThread().name}] $msg")
15 
16 suspend fun makeRequest(): String {
17     log("Making request...")
18     return suspendCoroutine { c ->
19         ForkJoinPool.commonPool().execute {
20             c.resume("Result of the request")
21         }
22     }
23 }
24 
displaynull25 fun display(result: String) {
26     log("Displaying result '$result'")
27 }
28 
<lambda>null29 fun main(args: Array<String>) = runBlocking(Dispatchers.Swing) {
30     try {
31         // suspend while asynchronously making request
32         val result = makeRequest()
33         // example.display result in UI, here Swing dispatcher ensures that we always stay in event dispatch thread
34         display(result)
35     } catch (exception: Throwable) {
36         // process exception
37     }
38 }
39 
40