0

I am working with InheritableThreadLocal and I know that when creating the children the ThreadLocal will have the default value the same as the parent thread value. But how can I maintain this when the parent's ThreadLocal value is modified?

Is there a way that when the parent's ThreadLocal is modified, all his children threads receive that modification?

EDIT: I have a boolean set on false in my parent thread. But I have a condition and when that happens I set the boolean to true and I need to do this also for all the children threads that were spawned by my initial thread.

Old Pro
  • 24,624
  • 7
  • 58
  • 106
dianap
  • 27
  • 1
  • 8
  • 2
    Could you please post here some code? I have no idea what are you trying to achieve. – Jan Krakora May 07 '13 at 07:52
  • What do you mean by inheritable ThreadLocal? Each thread has it's own reference to the variable in ThreadLocal object. It doesn't matter if the thread is spawn from another thread. The "child" thread doesn't inherit ThreadLocal variable. – Jan Krakora May 07 '13 at 08:08
  • I am using `InheritableThreadLocal` and this way the child thread inherits the value from the parent. – dianap May 07 '13 at 08:11

2 Answers2

1

InheritableThreadLocal only helps set the initial value in the child. It does not provide any other synchronization. You have to use normal thread synchronization methods to propagate the value.

That said, if you want all the children to have the same value of the parent both at start and when the parent changes, then why are you giving them all separate objects that need to be synchronized? Why not give the children a reference to the parent and have them all check the parent's value?

Old Pro
  • 24,624
  • 7
  • 58
  • 106
-1

I am working with inheritable ThreadLocal and I know that when creating the children the ThreadLocal will have the default value the same as the parent thread value.

If it's inherited, it's the same object. Everything is the same.

But how can i maintain this when the parent's ThreadLocal value modifies?

Maintain what? There's only one ThreadLocal object in existence here. However it behaves is how it behaves.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • By maintaining i was referring to keep the same value from the parent threadlocal to the children threadlocal, even though the parent's threadlocal value changed. As far as I know once that the child thread is created with the parent threadLocal value, they are not the same object (the parent threadLocal value and the child threadLocal value) – dianap May 07 '13 at 08:07
  • This isn't making much sense. If the `ThreadLocal` is inherited there is only one of them. No 'parent' and 'child' about it. – user207421 May 07 '13 at 08:24
  • However now that someone has kindly edited your question it is now clear that you are using `InheritableThreadLocal`; but I don't see anything here that isn't answered in the [Javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/InheritableThreadLocal.html). – user207421 May 08 '13 at 01:38