How is Java's ThreadLocal implemented under the hood?
问题 How is ThreadLocal implemented? Is it implemented in Java (using some concurrent map from ThreadID to object), or does it use some JVM hook to do it more efficiently? 回答1: All of the answers here are correct, but a little disappointing as they somewhat gloss over how clever ThreadLocal 's implementation is. I was just looking at the source code for ThreadLocal and was pleasantly impressed by how it's implemented. The Naive Implementation If I asked you to implement a ThreadLocal<T> class