File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
kotlinx-coroutines-core/src
main/kotlin/kotlinx/coroutines/experimental Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change 1
1
package kotlinx.coroutines.experimental
2
2
3
- import java.util.concurrent.Executors
4
3
import java.util.concurrent.ScheduledExecutorService
4
+ import java.util.concurrent.ScheduledThreadPoolExecutor
5
5
import java.util.concurrent.TimeUnit
6
6
import kotlin.coroutines.startCoroutine
7
7
8
+ val KEEP_ALIVE = java.lang.Long .getLong(" kotlinx.coroutines.ScheduledExecutor.keepAlive" , 50L )
9
+
8
10
internal val scheduledExecutor by lazy<ScheduledExecutorService > {
9
- Executors .newScheduledThreadPool(1 ) { r ->
10
- Thread (r, " kotlinx.coroutines.ScheduledExecutor" ).apply { isDaemon = true }
11
+ ScheduledThreadPoolExecutor (1 ) { r ->
12
+ Thread (r, " kotlinx.coroutines.ScheduledExecutor" )
13
+ }.apply {
14
+ setKeepAliveTime(KEEP_ALIVE , TimeUnit .MILLISECONDS )
15
+ allowCoreThreadTimeOut(true )
16
+ // "setRemoveOnCancelPolicy" is available only since JDK7, so try it via reflection
17
+ try {
18
+ val m = this ::class .java.getMethod(" setRemoveOnCancelPolicy" , Boolean ::class .javaPrimitiveType)
19
+ m.invoke(this , true )
20
+ } catch (ex: Throwable ) { /* ignore */ }
11
21
}
12
22
}
13
23
Original file line number Diff line number Diff line change
1
+ package examples
2
+
3
+ import kotlinx.coroutines.experimental.Here
4
+ import kotlinx.coroutines.experimental.delay
5
+ import kotlinx.coroutines.experimental.launch
6
+
7
+ fun main (args : Array <String >) {
8
+ launch(Here ) {
9
+ delay(1000L )
10
+ println (" World!" )
11
+ }
12
+ println (" Hello!" )
13
+ }
You can’t perform that action at this time.
0 commit comments