Michael Hixson Michael Hixson - 2 years ago 84
Java Question

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download