231. Hooking ScopedValue and virtual threads
The ScopedValue
API was added to handle the shortcomings of ThreadLocal
. But what are the shortcomings of ThreadLocal
?
Thread-local variables’ shortcomings
First of all, it is hard to say and track who’s mutating a thread-local variable. This is a shortcoming of the API design. Basically, a ThreadLocal
variable is globally available (at the application level or at a lower level), so it is hard to say from where it is mutated. Imagine that it is your responsibility to read, understand, and debug an application that uses several thread-local variables. How will you manage to follow the code logic and how will you know, at any given time, what values are stored by these thread-local variables? It would be a nightmare to track these variables from class to class and to signal when they mutated.
Second, thread-local variables may live forever or longer than they should. How is this possible? Thread-local variables...