Can I make ConcurrentHashMap<Thread, object> to store a thread’s data and use Thread.currentThread() to retreive it? I checked the Java source, and found that currentThread() is native and that equals is not overwritten.
Can I make ConcurrentHashMap<Thread, object> to store a thread’s data and use Thread.currentThread() to
Share
You could, but there is a built in mechanism ThreadLocal for just that purpose. The only reason to reinvent it is if you need to also access those values from some thread other than their “owner” in order to manipulate them.
Note that the initial value for every thread is null, but you can supply default values: