Thread Locals (was Re: State of Loom)
Andrew Haley
aph at redhat.com
Thu May 28 08:48:23 UTC 2020
On 27/05/2020 18:08, Douglas Surber wrote:
> Most of this code was written many, many years ago. We demonstrated
> that holding a Thingy in a static field resulted in excessive
> contention on the Thingy. Rather than invent a new data structure
> that we would have to maintain, we chose to leverage the tool that
> the Java class library provided, ie ThreadLocal. Could we do
> something different? Certainly. But having to roll our own increases
> our (already excessive) maintenance burden. I'd much rather use
> standard tools when possible, and in my use case ThreadLocal is
> acceptable. For now.
Sure, but that's ThreadLocals all over: they get used for many
purposes, few of which they're ideal for, but they're good enough. I
suppose that's why ThreadLocals are ubiquitous, even though they're
expensive and don't scale well. If we're to invent better replacements
(as I believe we should) then they will have to be narrower in scope.
> If the need arose to eliminate the ThreadLocals we would attempt to
> make the Thingys non-blocking. The benefit of that is eliminating
> the cost of having multiple Thingys. From experience making other
> parts of our code non-blocking I have reason to think that making
> these Thingys non-blocking would be challenging and would cause
> maintenance problems at least in the short term.
That's fair enough. I do sympathize. The world in which Threads are
cheap and plentiful inevitably looks different from one in which
they're not, and this will affect coding practices.
I think you've put your finger on something that will recur as Loom
becomes more widely known: people like the idea of lightweight threads
and want their benefits but don't like all of the consequences. These
consequences are the result of decades of not-quite-ideal decisions,
particularly around ThreadLocals.
--
Andrew Haley (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
More information about the loom-dev
mailing list