[Java 多线程] volatile关键字实现的简单读写锁

2017-01-05  本文已影响0人  oyealex

2017-1-5 14:46:58 oye
Example Code:

public class CheesyCounter {
    // Employs the cheap read-write lock trick
    // All mutative operations MUST be done with the 'this' lock held
    @GuardedBy("this")
    private volatile int value;

    public int getValue() { return value; }

    public synchronized int increment() {
        return value++;
    }
}

Analysis:

上一篇 下一篇

猜你喜欢

热点阅读