简体   繁体   中英

How to continue object from ThreadLocal in child thread?

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....

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM