使用无锁的方式和有锁的方式的程序性能对比
生活随笔
收集整理的這篇文章主要介紹了
使用无锁的方式和有锁的方式的程序性能对比
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這里分別使用有鎖和無(wú)鎖兩種方式,對(duì)一個(gè)數(shù)值進(jìn)行增加,一直增加到100000,然后輸出使用時(shí)間的長(zhǎng)短。
?
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 // 設(shè)置線程數(shù)量 9 private static int N = 1000; 10 // 設(shè)置最大值 11 private static int M = 1000000; 12 13 /** 14 * 無(wú)鎖方法 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 // 開(kāi)始時(shí)間 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 // 結(jié)束時(shí)間 31 long endTime = System.currentTimeMillis(); 32 System.out.println("無(wú)鎖線程數(shù)量為 : " + N + " 開(kāi)始時(shí)間為 : " + starttime 33 + " 結(jié)束時(shí)間為 : " + endTime + " 耗費(fèi)時(shí)間為 : " + (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("有鎖線程數(shù)量為 : " + N + " 開(kāi)始時(shí)間為 : " + starttime 55 + " 結(jié)束時(shí)間為 : " + endTime + " 耗費(fèi)時(shí)間為 : " + (endTime - starttime) 56 + "ms" + " value:" + AtomicRun.atomicInteger); 57 } 58 59 public static void main(String[] args) throws InterruptedException { 60 System.out.println("當(dāng)線程數(shù)量為 : " + N + "時(shí):"); 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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/Cilimer/p/4021210.html
總結(jié)
以上是生活随笔為你收集整理的使用无锁的方式和有锁的方式的程序性能对比的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C#值类型以及默认值记录下
- 下一篇: MySql默认编码所造成的乱码麻烦1.2