Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue object from ThreadLocal in child thread?

Tags:

java

I have passed one object in ThreadLocal. Now my current thread going to create new Child thread. I want object from ThreadLocal should continue with child thread also.

Is there any way to do so....?

Thank you in advance....

like image 214
user574557 Avatar asked Jun 13 '11 07:06

user574557


2 Answers

What you need is an InheritableThreadLocal. An InheritableThreadLocal is passed (Java "call by value" semantics) from the parent thread to a child thread when the latter is created.

like image 59
Stephen C Avatar answered Nov 15 '22 19:11

Stephen C


You may retrieve the object itself from your ThreadLocal via the get() method and pass this reference to you child thread.

If instead you want to share it with your child threads, see other answers.

like image 32
Howard Avatar answered Nov 15 '22 19:11

Howard