Skip to content
Advertisement

Using a synchronizedSet to synchronize access between two threads

I’m not able to synchronize two threads using a set:

private Set<String> set;
...
set = Collections.synchronizedSet(new HashSet<String>());

and passing it to two threads. One accessing:

synchronized (set) {
    // [1]
    if (set.contains(str)) {
    ...
    } else {
        // [3]
    }
}

and another updating:

synchronized (set) {
    set.add(str);   // [2]
...
}

What happens is that [1], [2], [3] happens in sequence. During [1], it is correct that the set doesn’t have yet the item I’m looking for. But then [2] updates it by adding the item. And during [3], I now see the item. How do I fix this? I also have a ConcurrentHashMap being shared by the same threads but it works perfectly fine. What is the set’s equivalent to ConcurrentHashMAp?

UPDATE: The code is too long. Anyway, my updated question is – What is the set’s equivalent to ConcurrentHAshMap?

Advertisement

Answer

You are synchronizing access correctly. Actually, wrapping it in synchronizedSet() is not having any additional effect here. There is no ConcurrentHashSet though you can kind of get the same thing from Collections.newSetFromMap() and ConcurrentHashMap. But this isn’t the problem.

The problem is somewhere else in your code. For example: are you sure you’re synchronizing on the same set? are your keys implementing hashCode() and equals() correctly? have you made them mutable (bad idea) and something is changing the keys?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement