forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TtlWrapperDemo.kt
42 lines (34 loc) · 1.21 KB
/
TtlWrapperDemo.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
41
42
package com.alibaba.demo.ttl
import com.alibaba.ttl.TransmittableThreadLocal
import com.alibaba.ttl.TtlCallable
import com.alibaba.ttl.TtlRunnable
import java.util.concurrent.Callable
import java.util.concurrent.Executors
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
fun main() {
val executorService = Executors.newCachedThreadPool()
val context = TransmittableThreadLocal<String>()
context.set("value-set-in-parent")
println("[parent thread] set ${context.get()}")
/////////////////////////////////////
// Runnable / TtlRunnable
/////////////////////////////////////
val task = Runnable { println("[child thread] get ${context.get()} in Runnable") }
val ttlRunnable = TtlRunnable.get(task)!!
executorService.submit(ttlRunnable).get()
/////////////////////////////////////
// Callable / TtlCallable
/////////////////////////////////////
val call = Callable {
println("[child thread] get ${context.get()} in Callable")
42
}
val ttlCallable = TtlCallable.get(call)!!
executorService.submit(ttlCallable).get()
/////////////////////////////////////
// cleanup
/////////////////////////////////////
executorService.shutdown()
}