CAS实现无锁模式

用多线程实现一个数字的自增长到1000000,分别用无锁模式和锁模式来实现代码.

1.使用ReentrantLock.

package test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadWithLock {

    private static final int THREAD_COUNT=3;
    private static volatile int value= 0;
    private static final ReentrantLock lock = new ReentrantLock();
    private static final CountDownLatch startSignal = new CountDownLatch(1);
    private static final CountDownLatch endSignal   = new CountDownLatch(1);
    
    public static void main(String[] args) throws InterruptedException{

        for(int i=0;i<=THREAD_COUNT;i++){
            (new Counter()).start();
        }
        //start to record time.
        long start = System.nanoTime();
        //send the started signal to all threads.
        startSignal.countDown();
        //wait for anyone to finish the counting.
        endSignal.await();
        long end = System.nanoTime();
        System.out.println(end-start);
    }

    static class Counter extends Thread {
        
        @Override
        public void run() {
            try {
                startSignal.await();
                while(true){
                    try {
                        lock.lock();
                        if(value<1000000){
                            value++;
                        }
                        else {
                            break;
                        }
                    }
                    finally{
                        lock.unlock();
                    }
                }
                endSignal.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }
}

使用CAS模式,这其实是一种乐观锁模式,它默认是没有竞争的,如果存在竞争,失败了让代码重试.

package test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadNoLock {

    private static final int THREAD_COUNT=3;
    private static final AtomicInteger value= new AtomicInteger(0);
    private static final CountDownLatch startSignal = new CountDownLatch(1);
    private static final CountDownLatch endSignal   = new CountDownLatch(1);
    
    public static void main(String[] args) throws InterruptedException{
        for(int i=0;i<THREAD_COUNT;i++){
            (new Counter()).start();
        }
        //start to record time.
        long start = System.nanoTime();
        //send the started signal to all threads.
        startSignal.countDown();
        //wait for anyone to finish the counting.
        endSignal.await();
        long end = System.nanoTime();
        System.out.println(end-start);
    }

    static class Counter extends Thread {
        
        @Override
        public void run() {
            try {
                startSignal.await();
                int current;
                while(true){
                    if((current = value.get()) <1000000){
                        value.compareAndSet(current, current+1);
                    }
                    else{
                        break;
                    }
                }
                endSignal.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }
}

 把Thread_count分别设置为1000,300,30,3 得到的性能数据如下,我们可以发现无锁模式的效率更高.

posted @ 2014-10-03 21:26  princessd8251  阅读(610)  评论(0编辑  收藏  举报