Thread-safe HashSet with Guava Collections

后端 未结 4 2263
攒了一身酷
攒了一身酷 2021-02-19 04:13

Like the title says, i would like to get a thread-safe HashSet using Guava Collections.
Are any available?

4条回答
  •  孤街浪徒
    2021-02-19 04:51

    Google Collections had a factory method named Sets.newConcurrentHashSet() for a while.

    Its implementation was similar to Chris's suggestion:

    public static  Set newConcurrentHashSet() {
      return newSetFromMap(new ConcurrentHashMap());
    }
    

    They had a newSetFromMap() method inside the com.google.common.collect.Sets class (written by Doug Lea with assistance from members of JCP JSR-166). That method was added to java.util.Collections in java 1.6.

    It was withdrawn in Google Collections 1.0rc1, since there are plans to better support concurrent sets in Guava (more information here).

    This post expands on the use of the "newSetFromMap" method to construct concurrent sets.

提交回复
热议问题