java CAS算法

CAS算法是硬件对于并发操作的支持,其中包含了三个操作数:内存值,预估值和更新值。没当要执行更新操作时,会先在同步方法中比较内存值和预估值是否相等,如果相等才会用更新值替换内存值,否则什么也不做。 

 

package concurrent;

/*
 * 模拟CAS算法
 */

class CAS {
    // 内存值
    private volatile int value;

    // 获取内存值
    public synchronized int getValue() {
        return value;
    }

    //无论更新成功还是失败,都会返回旧的内存值
    public synchronized int compareAndSwap(int expectedValue, int newValue) {
        int oldValue = value;

        if (oldValue == expectedValue) {
            this.value = newValue;
        }

        return oldValue;
    }

    //判断更新是否成功,如果更新成功,旧的内存值会和预估值相等
    public synchronized boolean compareAndSet(int expectedValue, int newValue) {
        return expectedValue == compareAndSwap(expectedValue, newValue);
    }
}

public class TestCAS {

    public static void main(String[] args) {

        CAS cas = new CAS();

        //创建10个线程来模拟多线程环境
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    int expectedValue = cas.getValue();
                    boolean b = cas.compareAndSet(expectedValue, (int) (Math.random() * 101));
                    System.out.println(b);
                }
            }).start();
        }
    }
}

 

posted @ 2018-07-25 11:40  全力以赴001  阅读(303)  评论(0编辑  收藏  举报