Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个单例类,我需要存储一些特定于每个线程的字段。我正在考虑将这些字段作为 ThreadLocal 变量添加到单例中,或者使用同步的哈希表,其中键是线程 ID。关于哪个开销较低的任何建议?我之前看到了性能比较,但这并不是我所需要的。
ThreadLocal 本质HashMap上是线程 ID 上的键控。你最好使用ThreadLocal.
ThreadLocal
HashMap
还有(我相信)使用 ThreadLocals 在幕后为您进行进一步的优化。