Search code examples
javaguava

Is a Guava Table thread safe when its backing maps are thread safe?


Will Guava's Tables.newCustomTable(Map, Supplier) method return thread safe tables when supplied with thread safe maps? For example:

public static <R, C, V> Table<R, C, V> newConcurrentTable() {
  return Tables.newCustomTable(
      new ConcurrentHashMap<R, Map<C, V>>(),
      new Supplier<Map<C, V>>() {
        public Map<C, V> get() {
          return new ConcurrentHashMap<C, V>();
        }
      });
}

Does that code actually return concurrent tables?


Solution

  • From the doc: "If multiple threads access this table concurrently and one of the threads modifies the table, it must be synchronized externally."

    Concurrent backing collections aren't enough.