Are there thread group-local variables in Java?

前端 未结 3 1481
失恋的感觉
失恋的感觉 2020-12-29 13:40

I am looking for a class similar to ThreadLocal which would work on thread groups instead of threads.

If there is not such a class (in some open source library) how

3条回答
  •  [愿得一人]
    2020-12-29 14:39

    ThreadGroup is rarely used, so there isn't platform support.

    Using [Weak/Identity/Concurrent]HashMap will just about work, if not very fast. You really want the map to be all weak, identity and concurrent but with the Java library you only get to choose one, currently.

    To improve performance, note that Threads do not change ThreadGroup. Therefore, cache the value with a ThreadLocal (override initialValue). ThreadLocal has good performance (a couple dozen cycles per get).

提交回复
热议问题