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

  • 相关阅读:
    linux中上传文件出现Refused to display 'http://***' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
    1 js中常用的操作
    1 走进并行世界
    18 java I/O 系统
    Spring项目中的数据库加密
    13/14:字符串与类型信息
    使用HttpClient访问接口(Rest接口和普通接口)
    java 队列的使用(转载)
    java锁有哪些类(转)
    J2EE,J2SE,J2ME,JDK,SDK,JRE,JVM区别(转载)
  • 原文地址:https://www.cnblogs.com/princessd8251/p/4005354.html
Copyright © 2011-2022 走看看