0

This question is related to one I just asked here.

Givenservers is a private static ConcurrentHashMap, are the following two methods and the third approach all equivalent in that the servers map will reflect the change made by all three?

1.

public static synchronized int releaseConnection(Server s) {
    return servers.get(s.getId()).decrementConns();
}

2.

public static synchronized int releaseConnection(Server s) {
    return s.decrementConns();
}


3. just call decrementConns() where its needed (in other classes)?

Community
  • 1
  • 1
foamroll
  • 752
  • 7
  • 23
  • 1
    What if it isn't in the map? – SLaks Aug 28 '13 at 17:05
  • 3
    Also, don't use `synchronized` methods; they don't provide appropriate levels of granularity. – SLaks Aug 28 '13 at 17:06
  • If the instance in the map (given it is found there, of course, as SLaks pointed out!) and the instance given as an argument are the same, the two are equivalent. – ppeterka Aug 28 '13 at 17:07

1 Answers1

1

3rd way is best and should be sufficient. All 3 will do the job but 1 and 2 are not needed as you are working with ConcurrentHashMap.

Lokesh
  • 7,810
  • 6
  • 48
  • 78