java - Can a child thread modify its parent's Threadlocal variable? -


i have 2 threads each threadlocal list named threadlocal both spawn child threads. want child threads able modify parent's threadlocal.

i have tried passing in parent child can call parent.threadlocal.get().add(x) causes null pointer exception. when parent calls threadlocal.get().add(x) able add x list fine.

i know issue .add(x) because having child call .get() not cause exception. tried passing threadlocal child , gives same error.

is there way this?

you can retrieve list in parent , pass child, or use inheritablethreadlocal.

using inheritablethreadlocal means value copied child threads.

note in either case, can't change value of parent's threadlocal variable, can mutate object variable refers to. in case calling .add(...) on list work fine, long handle concurrency correctly, e.g. using copyonwritearraylist


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -