forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TtlExecutorWrapperDemo.kt
40 lines (34 loc) · 1.19 KB
/
TtlExecutorWrapperDemo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.alibaba.demo.ttl
import com.alibaba.ttl.TransmittableThreadLocal
import com.alibaba.ttl.threadpool.TtlExecutors
import java.util.concurrent.Callable
import java.util.concurrent.Executors
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
fun main() {
val ttlExecutorService = Executors.newCachedThreadPool().let {
// return TTL wrapper from normal ExecutorService
TtlExecutors.getTtlExecutorService(it)
}!!
val context = TransmittableThreadLocal<String>()
context.set("value-set-in-parent")
println("[parent thread] set ${context.get()}")
/////////////////////////////////////
// Runnable
/////////////////////////////////////
val task = Runnable { println("[child thread] get ${context.get()} in Runnable") }
ttlExecutorService.submit(task).get()
/////////////////////////////////////
// Callable
/////////////////////////////////////
val call = Callable {
println("[child thread] get ${context.get()} in Callable")
42
}
ttlExecutorService.submit(call).get()
/////////////////////////////////////
// cleanup
/////////////////////////////////////
ttlExecutorService.shutdown()
}