Skip to content

Instantly share code, notes, and snippets.

@squito
Last active May 1, 2018 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squito/8fc6533b1eeeb48559302c5898ae2c1d to your computer and use it in GitHub Desktop.
Save squito/8fc6533b1eeeb48559302c5898ae2c1d to your computer and use it in GitHub Desktop.
InheritableThreadLocals
creating a new thread pool thread 0 : tl = null
creating a new thread pool thread 1 : tl = null
creating a new thread pool thread 2 : tl = null
creating a new thread pool thread 3 : tl = null
creating a new thread pool thread 4 : tl = null
creating a new thread pool thread 5 : tl = null
creating a new thread pool thread 6 : tl = null
creating a new thread pool thread 7 : tl = null
creating a new thread pool thread 8 : tl = null
creating a new thread pool thread 9 : tl = null
Ran task 2; thread = pool thread 2; tl = null
Ran task 0; thread = pool thread 0; tl = null
Ran task 1; thread = pool thread 1; tl = null
Ran task 3; thread = pool thread 3; tl = null
Ran task 4; thread = pool thread 4; tl = null
Ran task 5; thread = pool thread 5; tl = null
Ran task 6; thread = pool thread 6; tl = null
Ran task 9; thread = pool thread 8; tl = null
Ran task 7; thread = pool thread 7; tl = null
Ran task 8; thread = pool thread 9; tl = null
Ran task 11; thread = pool thread 9; tl = null
Ran task 17; thread = pool thread 2; tl = null
Ran task 16; thread = pool thread 3; tl = null
Ran task 15; thread = pool thread 4; tl = null
Ran task 13; thread = pool thread 7; tl = null
Ran task 12; thread = pool thread 8; tl = null
Ran task 14; thread = pool thread 5; tl = null
Ran task 10; thread = pool thread 6; tl = null
Ran task 19; thread = pool thread 0; tl = null
Ran task 18; thread = pool thread 1; tl = null
creating a new thread pool thread 10 : tl = foo
creating a new thread pool thread 11 : tl = foo
creating a new thread pool thread 12 : tl = foo
creating a new thread pool thread 13 : tl = foo
creating a new thread pool thread 14 : tl = foo
creating a new thread pool thread 15 : tl = foo
creating a new thread pool thread 16 : tl = foo
creating a new thread pool thread 17 : tl = foo
creating a new thread pool thread 18 : tl = foo
creating a new thread pool thread 19 : tl = foo
Ran task 29; thread = pool thread 2; tl = null
Ran task 22; thread = pool thread 1; tl = null
Ran task 24; thread = pool thread 0; tl = null
Ran task 21; thread = pool thread 6; tl = null
Ran task 20; thread = pool thread 7; tl = null
Ran task 25; thread = pool thread 5; tl = null
Ran task 30; thread = pool thread 10; tl = foo
Ran task 26; thread = pool thread 3; tl = null
Ran task 23; thread = pool thread 8; tl = null
Ran task 27; thread = pool thread 4; tl = null
Ran task 28; thread = pool thread 9; tl = null
Ran task 36; thread = pool thread 14; tl = foo
Ran task 39; thread = pool thread 19; tl = foo
Ran task 38; thread = pool thread 18; tl = foo
Ran task 34; thread = pool thread 11; tl = foo
Ran task 33; thread = pool thread 15; tl = foo
Ran task 31; thread = pool thread 16; tl = foo
Ran task 35; thread = pool thread 13; tl = foo
Ran task 37; thread = pool thread 12; tl = foo
Ran task 32; thread = pool thread 17; tl = foo
Ran task 40; thread = pool thread 13; tl = foo
Ran task 48; thread = pool thread 14; tl = foo
Ran task 49; thread = pool thread 9; tl = null
Ran task 41; thread = pool thread 12; tl = foo
Ran task 43; thread = pool thread 16; tl = foo
Ran task 47; thread = pool thread 19; tl = foo
Ran task 42; thread = pool thread 15; tl = foo
Ran task 45; thread = pool thread 18; tl = foo
Ran task 46; thread = pool thread 11; tl = foo
Ran task 44; thread = pool thread 17; tl = foo
Ran task 52; thread = pool thread 12; tl = foo
Ran task 55; thread = pool thread 15; tl = foo
Ran task 53; thread = pool thread 11; tl = foo
Ran task 54; thread = pool thread 19; tl = foo
Ran task 58; thread = pool thread 13; tl = foo
Ran task 57; thread = pool thread 9; tl = null
Ran task 59; thread = pool thread 14; tl = foo
Ran task 51; thread = pool thread 16; tl = foo
Ran task 50; thread = pool thread 18; tl = foo
Ran task 56; thread = pool thread 17; tl = foo
import java.util.concurrent._
import java.util.concurrent.atomic._
val tl = new InheritableThreadLocal[String]()
tl.get()
val die = new AtomicBoolean(false)
val threadIdx = new AtomicInteger(0)
val threadFactory = new ThreadFactory() {
val default = Executors.defaultThreadFactory()
def newThread(runnable: Runnable): Thread = {
val name = s"pool thread ${threadIdx.getAndIncrement()}"
println(s"creating a new thread $name : tl = ${tl.get()}")
val t = default.newThread(runnable)
t.setName(name)
t
}
}
val pool = Executors.newCachedThreadPool(threadFactory)
val idx = new AtomicInteger(0)
def submitTask(taskTime: Long = 1000): Unit = {
pool.submit(new Runnable(){
def run(): Unit = {
val tname = Thread.currentThread().getName()
Thread.sleep(taskTime)
if (die.compareAndSet(true, false)) {
println(s"failing in ${tname}")
throw new RuntimeException("failed thread!")
} else {
println(s"Ran task ${idx.getAndIncrement()}; thread = $tname; tl = ${tl.get()}")
}
}
})
}
(0 until 10).foreach { _ => submitTask() }
Thread.sleep(1000)
tl.set("foo")
// tl is still null to all task threads
(0 until 10).foreach { _ => submitTask() }
Thread.sleep(1000)
// now submit a bunch of tasks, so our pool is likely to create new threads
(0 until 20).foreach { _ => submitTask() }
Thread.sleep(1000)
// but if we update again, that thread still sees the old value.
// though the thread pool is really free to do whatever it wants,
// it may have killed the other threads in the meantime if they
// were idle.
tl.set("blah")
(0 until 10).foreach { _ => submitTask() }
// or if we sleep a while, our thread pool may kill the threads, and then
// create new ones when we submit new tasks. But it gets to do this however
// it likes
Thread.sleep(10000)
(0 until 10).foreach { _ => submitTask() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment