日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/2 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用无锁的方式和有锁的方式的程序性能对比 小編覺(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。