I was reading about ThreadLocal class when I came across the below statement by Joshua Bloch multiple times..
“Sleazing” values through callbacks that you don’t control: sometimes you must call a library method that calls back into your package. At this point, you need some context that you were unable to pass to yourself, due to deficiencies in the library. In this rare situation, thread locals can be a lifesaver.
Having a hard time understanding this one..Would appreciate if someone could elaborate and explain
Thanks..Heena
My application calls a library that searches stored data.
That library also has an interface you can implement that tells it how to convert the raw results of a search into the data format that you want.
My implementation of how to convert search results into the objects I want, involves knowing a particular time zone.
So the process is, (A) I call library search method, (B) search method then calls my other piece of code that converts raw results into my format, (C) my conversion requires knowing a timezone. The challenge is how do I make information, the timezone, that I know at point A, available at point C?
Obviously the methods that the library offers don't include passing around a time zone, so I stick it in a ThreadLocal!