使用无锁的方式和有锁的方式的程序性能对比

这里分别使用有锁和无锁两种方式,对一个数值进行增加,一直增加到100000,然后输出使用时间的长短。

 

  1 import java.util.concurrent.ExecutorService;

  2 import java.util.concurrent.Executors;

  3 import java.util.concurrent.TimeUnit;

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

  5 

  6 public class TestAtomic {

  7 

  8     // 设置线程数量

  9     private static int N = 1000;

 10     // 设置最大值

 11     private static int M = 1000000;

 12 

 13     /**

 14      * 无锁方法

 15      * 

 16      * @throws InterruptedException

 17      */

 18     private static void atomicMethod() throws InterruptedException {

 19         AtomicRun atomicRun = new AtomicRun();

 20         AtomicRun.endValue = M;

 21 

 22         ExecutorService service = Executors.newFixedThreadPool(N);

 23         // 开始时间

 24         long starttime = System.currentTimeMillis();

 25         for (int i = 0; i < N; i++) {

 26             service.submit(atomicRun);

 27         }

 28         service.shutdown();

 29         service.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);

 30         // 结束时间

 31         long endTime = System.currentTimeMillis();

 32         System.out.println("无锁线程数量为 : " + N + " 开始时间为 : " + starttime

 33                 + " 结束时间为 : " + endTime + " 耗费时间为 : " + (endTime - starttime)

 34                 + "ms" + " value:" + AtomicRun.atomicInteger);

 35     }

 36 

 37     /**

 38      * 加锁方法

 39      * 

 40      * @throws InterruptedException

 41      */

 42     private static void synMethod() throws InterruptedException {

 43         SynRun synRun = new SynRun();

 44         SynRun.endValue = M;

 45 

 46         ExecutorService service = Executors.newFixedThreadPool(N);

 47         long starttime = System.currentTimeMillis();

 48         for (int i = 0; i < N; i++) {

 49             service.submit(synRun);

 50         }

 51         service.shutdown();

 52         service.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);

 53         long endTime = System.currentTimeMillis();

 54         System.out.println("有锁线程数量为 : " + N + " 开始时间为 : " + starttime

 55                 + " 结束时间为 : " + endTime + " 耗费时间为 : " + (endTime - starttime)

 56                 + "ms" + " value:" + AtomicRun.atomicInteger);

 57     }

 58 

 59     public static void main(String[] args) throws InterruptedException {

 60         System.out.println("当线程数量为 : " + N + "时:");

 61         atomicMethod();

 62         synMethod();

 63     }

 64 }

 65 

 66 /**

 67  * 

 68  * @author 秦孔祥

 69  * 

 70  */

 71 class AtomicRun implements Runnable {

 72 

 73     protected static AtomicInteger atomicInteger = new AtomicInteger();

 74     protected static int endValue;

 75 

 76     @Override

 77     public void run() {

 78         int startValue = atomicInteger.get();

 79         while (startValue < endValue) {

 80             startValue = atomicInteger.incrementAndGet();

 81         }

 82     }

 83 }

 84 

 85 class SynRun implements Runnable {

 86 

 87     protected static int startValue;

 88     protected static int endValue;

 89 

 90     @Override

 91     public void run() {

 92         while (startValue < endValue) {

 93             addValue();

 94         }

 95     }

 96 

 97     private synchronized void addValue() {

 98         startValue++;

 99     }

100 }

 

你可能感兴趣的:(性能)