[concurrency-interest] HalfSync

Hanson Char hanson.char at gmail.com
Wed May 24 17:07:20 EDT 2006


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 ?

Hanson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060524/9e6bdd85/attachment.html 


More information about the Concurrency-interest mailing list