Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to ThreadLocal

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?

like image 519
prem Avatar asked Sep 04 '26 20:09

prem


1 Answers

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.

like image 122
Nathan Hughes Avatar answered Sep 07 '26 08:09

Nathan Hughes