I was reading the JavaDoc for Threadlocal here
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ThreadLocal.html
and it says
“ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID). “
But my question is why did they choose to make it static (typically) – it makes things a bit confusing to have “per thread” state but the fields are static?
Because if it were an instance level field, then it would actually be “Per Thread – Per Instance”, not just a guaranteed “Per Thread.” That isn’t normally the semantic you’re looking for.
Usually it’s holding something like objects that are scoped to a User Conversation, Web Request, etc. You don’t want them also sub-scoped to the instance of the class.
One web request => one Persistence session.
Not one web request => one persistence session per object.