CountDownLatch achieve a fair measure performance under lock cas, synchronized and ReenTrantLock concurrency

1.cas

It's very simple to achieve, is to be compared with an expected value and memory values, if two values ​​are equal, it is replaced with a memory value expected value and returns true. Otherwise, it returns false.

2. synchronized way three applications of
Java every object can be used as a lock, which is synchronized basis synchronized implementation:
    general synchronization method (instance methods), lock the current instance of the object, before entering the synchronization code to get the current instance lock
    static synchronization method, the lock current class is an object class, in the synchronization code to be obtained before the current class object lock
    synchronization method block inside the brackets is the lock object to a given object locking, in the synchronization code to be obtained prior to a given library lock objects

3. ReenTrantLock implement the interface Lock

ReentrantLockIt is a mutex, is a reentrant lock (Reentrant is re-entering the meaning). ReentrantLockLock locks held by only one thread at the same point in time, but it may be several times to get a single thread, every time get AQSthe stateis incremented every time the release statedecrements. Remember synchronizedWell, it is also reentrant, another method of synchronizing a synchronous method call is not a problem.

Ado, on the code:

  . 1  public  static  void main (String [] args) throws Exception {
   2          a CountDownLatch CountDownLatch = new new a CountDownLatch ( . 1 ); // fairer measurement
   3          // global variables used when: the key role of volatile promoter is a modified class of global variable 
  . 4          int K = 0 , m = 0 , n-= 0 , P = 0 ;
   . 5          Lock Lock = new new of ReentrantLock ( to false );
   . 6          Lock lockFair = new new of ReentrantLock ( to true );
   . 7          Long Time =System.currentTimeMillis();
  8         //无锁
  9         Run run = new Run(k,time,countDownLatch);
 10         //sync锁
 11         RunSyn runSyn = new RunSyn(p,time,countDownLatch);
 12         //reentrantLock 非公平锁
 13         RunLock runLock = new RunLock(m,lock,time,"unfair",countDownLatch);
 14         //reentrantLock  公平锁
 15         RunLock runLockFair = new RunLock(m,lockFair,time,"fair",countDownLatch);
 16         //cas 锁
 17         RunCas runCas = new RunCas(n,new AtomicInteger(),time,countDownLatch);
 18         for (int i = 0; i < 3; i++) {
 19             new Thread(runLockFair).start();
 20             new Thread(run).start();
 21             new Thread(runLock).start();
 22             new Thread(runCas).start();
 23             new Thread(runSyn).start();
 24         }
 25         System.out.println ( " ---------" kick "-------- " );
 26          countDownLatch.countDown (); // start all blocked thread
 27      }
 28  
29      / * *
 30       * no lock
 31 is       * / 
32      static  class the Run the implements the Runnable {
 33 is          int K;
 34 is          Long Time;
 35          a CountDownLatch CountDownLatch;
 36          public the Run ( int J, Long Time, a CountDownLatch CountDownLatch) {
 37 [              the this .K = J;
 38 is              the this.time = time;
 39             this.countDownLatch = countDownLatch;
 40         }
 41         @Override
 42         public void run() {
 43             try {
 44                 countDownLatch.await();
 45             } catch (InterruptedException e) {
 46                 e.printStackTrace();
 47             }
 48             for (int i = 0; i < 1000000; i++) {
 49                 k++;
 50                 //System.out.println("i--->"+i);
 51             }
 52             System.out.println("无锁 k--->"+k+"---->"+(System.currentTimeMillis()-time));
 53         }
 54     }
 55 
 56     /**
 57      * lock 锁
 58      */
 59      static class RunLock implements Runnable{
 60         int m;
 61         String sync;
 62         Long time;
 63         Lock lock;
 64         CountDownLatch countDownLatch;
 65         public RunLock(int j,Lock lock,Long time,String sync,CountDownLatch countDownLatch){
 66             this.m = j;
 67             this.time = time;
 68             this.lock = lock;
 69             this.sync = sync;
 70             this.countDownLatch = countDownLatch;
 71         }
 72         @Override
 73         public void run() {
 74             try {
 75                 countDownLatch.await();
 76             } catch (InterruptedException e) {
 77                 e.printStackTrace();
 78             }
 79             for (int i = 0; i < 1000000; i++) {
 80                 lock.lock();
 81                 m++;
 82                 lock.unlock();
 83             }
 84             System.out.println(sync+"lock m--->"+m+"---->"+(System.currentTimeMillis()-time));
 85         }
 86     }
 87 
 88     /**
 89      * CAS
 90      */
 91     static class RunCas implements Runnable{
 92         AtomicInteger atomicInteger;
 93         int n;
 94         Long time;
 95         CountDownLatch countDownLatch;
 96         public RunCas(int j,AtomicInteger atomicInteger,Long time,CountDownLatch countDownLatch){
 97             this.n = j;
 98             this.time = time;
 99             this.atomicInteger = atomicInteger;
100             this.countDownLatch = countDownLatch;
101         }
102         @Override
103         public void run() {
104             try {
105                 countDownLatch.await();
106             } catch (InterruptedException e) {
107                 e.printStackTrace();
108             }
109             for (int i = 0; i < 1000000; i++) {
110                 getLock(atomicInteger);
111                 n++;
112                 unLock(atomicInteger);
113             }
114             System.out.println("cas n--->"+n+"---->"+(System.currentTimeMillis()-time));
115         }
116     }
117 
118     /**
119      * 获取锁
120      * @param atomicInteger
121      * @return
122      */
123     private static boolean getLock(AtomicInteger atomicInteger) {
124         while (true){
125             boolean flag = atomicInteger.compareAndSet(0,1);
126             if (flag){
127                 return true;
128             }
129         }
130     }
131 
132     /**
133      * 释放锁
134      * @param atomicInteger
135      * @return
136      */
137     private static boolean unLock(AtomicInteger atomicInteger) {
138         while (true){
139             boolean flag = atomicInteger.compareAndSet(1,0);
140             if (flag){
141                 return true;
142             }
143         }
144     }
145 
146     /**
147      * synchronized
148      */
149     static class RunSyn implements Runnable{
150         int p;
151         Long time;
152         CountDownLatch countDownLatch;
153         public RunSyn(int j,Long time,CountDownLatch countDownLatch){
154             this.p = j;
155             this.time = time;
156             this.countDownLatch = countDownLatch;
157         }
158         @Override
159         public void run() {
160             try {
161                 countDownLatch.await();//阻塞线程
162             } catch (InterruptedException e) {
163                 e.printStackTrace();
164             }
165             synchronized(this){
166                 for (int i = 0; i < 1000000; i++) {
167                     p++;
168                 }
169             }
170             System.out.println("synchronized p--->"+p+"---->"+(System.currentTimeMillis()-time));
171         }
172     }

 

result:

--------- "start the race" -------- 
the synchronized the p- ---> 1007776 ----> 15 
the synchronized the p- ---> 2000000 ----> 15 
lock-k ---> 246073 ----> 31 
lock-K ---> 1,108,336 ----> 46 is 
lock-K ---> 1247421 ----> 46 is 
the synchronized P ---> 3,000,000 ---- > 46 is 
unfairlock m ---> 2,824,215 ----> 109 
unfairlock m ---> 2,959,808 ----> 109 
unfairlock m ---> 3,000,000----> 109 
Case ---> 2,961,861 ----> 375 
Case ---> 2,961,864 ----> 375 
Case ---> 3000000 ----> 375 
fairlock m --- > 2995778 ----> 32035 
fairlock m ---> 3000000 ----> 32082 
fairlock m ---> 3000000 ----> 32082

 Three threads each thread accumulate one million, the results seen from the lock-free concurrency issues arise, synchronized performance of the best non-lock unfairlock fair and equitable performance cas lock fairlock almost poor performance

 

Guess you like

Origin www.cnblogs.com/wushenghfut/p/11830538.html