[concurrency-interest] HalfSync

Dawid Kurzyniec dawidk at mathcs.emory.edu
Wed May 24 19:57:17 EDT 2006


Hanson Char wrote:
> Consider the code:
>
> // Only the write is synchronized, not the read
> public class HalfSync {
>     private volatile int count;
>    
>     public HalfSync(int count) {
>         this.count = count;
>     }
>
>     public int getCount() {
>         return count;
>     }
>
>     public synchronized int increment(int delta) {
>         return this.count += delta;
>     }
> }
>
> As far as I can tell, this code is as thread-safe as it would be the 
> case if both the increment() and getCount() are synchronized, but 
> would allow higher level of concurrency.  (Similar to 
> CopyOnWriteArrayList).
>
> How would HalfSync compared to AtomicInteger ?  Which one should 
> perform faster ?
>

BTW. This is exactly how atomics are implemented in the backport.

Regards,
Dawid



More information about the Concurrency-interest mailing list