Internet is filled with articles on disadvantages of ThreadLocal in Java, and yet people keep using it in Web Development. What I did not find is, is there any alternative to threadlocals in web application?
JDK 20 includes an alternative to ThreadLocals called Scoped Values. This gives a way to specify storing values in a specified scope so that other code in the same thread (or in other threads that call it) can access them. Unlike ThreadLocal the scoped value is immutable once set so downstream code can't tamper with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With