• 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 得到的性能数据如下,我们可以发现无锁模式的效率更高.

  • 相关阅读:
    工作错误记录javac找不到
    蜜柚系统离备份文件解密
    如何备份蜜柚桌面!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Excel表导入数据库
    ReportViewer和 rdlc报表使用小记
    自定义天气WCF服务接口
    Windows平台下SVN安装配置及使用
    SQLServer 2012 Always on配置全过程
    处理check选中后鼠标悬停问题
    js MD5加密
  • 原文地址:https://www.cnblogs.com/princessd8251/p/4005354.html
Copyright © 2020-2023  润新知