commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Torsten Curdt <>
Subject Re: concurrency and starting a synchronized block
Date Thu, 10 Feb 2005 13:45:31 GMT
>>Consider this code...
>>  Object o = map.get(key);
>>  if (o == null) {
>>    synchronized(map) {
>>      map.put(key,new Object());
>>    }
>>  }
>>99% of the time the "get"s on the map are going
>>to return an object. That's why I would like
>>to avoid synchronizing the "get" access.
>>Now since a "put" might corrupt the data it
>>has to be synchronized.
> then get() twice:


> since 99% of the time it will not enter the block, the second lookup does not matter

that's true ...but that's not the problem :)

as I said: I don't mind if I loose an instance
but the problem is that the "get" is not
synchronized while the "put" is.

So I asssume that the "get" is not checking
for a lock on the "map" object which means
that a concurrent "get" and "put" might clash.

The above code would only be safe for two
concurrent "put" calls. least that's what I assume


View raw message