色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

java無鎖并發

瀏覽:118日期:2023-12-11 17:17:16

問題描述

下面代碼里無鎖和有鎖比是更好的實現嗎?我用jmeter每秒20個請求,無鎖代碼執行test()里的sleep操作的輸出大部分與500毫秒差別巨大,而有鎖代碼的輸出基本就是500毫秒相差1,2毫秒的樣子,這個問題很怪異啊....

@Controller@RequestMapping('/bench/')public class BenchController { @Autowired private FlowService flowService; private static Object[] lockObj; private static AtomicReference<Integer>[] locks; static {lockObj = new Object[100];for (int i = 0; i < lockObj.length; i++) { lockObj[i] = new Object();}locks = new AtomicReference[100];for (int i = 0; i < locks.length; i++) { locks[i] = new AtomicReference<Integer>(null);} } @RequestMapping('a') @ResponseBody public long a(int id) throws Exception {long start = System.currentTimeMillis();int index = id % 100;long inner=0;synchronized (lockObj[index]) { inner=test();}long result = System.currentTimeMillis() - start;System.out.println('all: '+result+' inner: '+inner);return result; } @RequestMapping('b') @ResponseBody public long b(int id) throws Exception {long start = System.currentTimeMillis();AtomicReference<Integer> lock=locks[id % 100];while (!lock.compareAndSet(null, id)) {}long inner=test();boolean flag=lock.compareAndSet(id, null);long result = System.currentTimeMillis() - start;System.out.println('all: '+result+' inner: '+inner+' flag:'+flag);return result; } public long test()throws Exception{long innerstart = System.currentTimeMillis();Thread.sleep(500);System.out.println(System.currentTimeMillis()-innerstart);return System.currentTimeMillis()-innerstart; }}

問題解答

回答1:

1.首先,明確兩個問題,synchronized 一般不是跟AtomicXX類進行比較,更多的是跟ReentrantLock這個類進行比較,網上關于這2者的比較很多,可以自行google之。

2.問題中關于無鎖跟有鎖的疑問,測試代碼b中的代碼是有問題的,

對于方法a,synchronized代碼塊來說,鎖被第一個進來的線程持有后,后續線程請求獲取鎖會被阻塞掛起,直到前面一個線程釋放鎖,后續的線程會恢復執行,由于鎖的存在,20個請求類似于順序執行,這一層由jvm調度

對于方法b,cas操作是非阻塞的,方法中的while循環其實是一直在執行(不斷嘗試進行cas操作),而我們知道,死循環是會消耗cpu資源的,并發數越多,線程越多,此處的cas操作越多,必然導致cpu使用率飆升,方法b中的代碼由jmeter測試的時候理論上來說應該一直由20個活躍的工作線程存在,cpu與線程模型是另外一個話題,線程數的調優是jvm一個比較高級的話題,感興趣可以自行google之

說說ReentrantLock與synchronized:通常情況下在高并發下,ReentrantLock比synchronized擁有更好的性能,而且ReentrantLock提供來一些synchronized并不提供的功能(鎖超時自動放棄等),示例代碼中可以減少sleep的時間,從而模擬更短停頓,更高的并發,500ms對于人來說很短,對于cpu來說基本就是天文數字了,基本用“慢如蝸牛”來形容也不為過,修改類一下示例代碼:

package com.gzs.learn.springboot;import java.util.LinkedList;import java.util.Random;import java.util.concurrent.atomic.AtomicReference;import java.util.concurrent.locks.LockSupport;import java.util.concurrent.locks.ReentrantLock;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping('/bench/')public class BenchController { private Random random = new Random(); private static Object[] lockObj; private static AtomicReference<Integer>[] locks; private static ReentrantLock[] reentrantLocks; static {lockObj = new Object[100];for (int i = 0; i < lockObj.length; i++) { lockObj[i] = new Object();}locks = new AtomicReference[100];for (int i = 0; i < locks.length; i++) { locks[i] = new AtomicReference<Integer>(null);}reentrantLocks = new ReentrantLock[100];for (int i = 0; i < reentrantLocks.length; i++) { reentrantLocks[i] = new ReentrantLock();} } @RequestMapping('a/{id}') @ResponseBody public long a(@PathVariable('id') int id) throws Exception {long start = System.currentTimeMillis();int index = id % 100;long inner = 0;synchronized (lockObj[index]) { inner = test();}long result = System.currentTimeMillis() - start;System.out.println('all: ' + result + ' inner: ' + inner);return result; } @RequestMapping('b/{id}') @ResponseBody public long b(@PathVariable('id') int id) throws Exception {long start = System.currentTimeMillis();id = id % 100;AtomicReference<Integer> lock = locks[id];int b = 0;while (!lock.compareAndSet(null, id)) { b = 1 + 1;}long inner = test();boolean flag = lock.compareAndSet(id, null);long result = System.currentTimeMillis() - start;System.out.println('all: ' + result + ' inner: ' + inner + ' flag:' + flag);System.out.println(b);return result; } @RequestMapping('c/{id}') @ResponseBody public long c(@PathVariable('id') int id) throws Exception {long start = System.currentTimeMillis();id = id % 100;ReentrantLock lock = reentrantLocks[id];lock.lock();long inner = test();lock.unlock();long result = System.currentTimeMillis() - start;System.out.println('all: ' + result + ' inner: ' + inner);return result; } public long test() throws Exception {long innerstart = System.currentTimeMillis();Thread.sleep(0, 100);// Thread.sleep(500);System.out.println(System.currentTimeMillis() - innerstart);return System.currentTimeMillis() - innerstart; }}

方法c是用ReentrantLock實現的,絕大多少情況下ReentrantLock比synchronized高效

juc(java.util.concurrent)中的核心類Aqs(AbstractQueuedSynchronizer)是一個基于隊列的 并發包,默認線程在鎖競爭(自旋)超過1000納秒的時候會被park(掛起操作),從而減少cpu頻繁的線程切換,可以嘗試調整方法c中的sleep的時間參數。

測試方法,本機沒有裝jmeter,用apache ab做的測試,測試命令:

ab -n 100 -c 20 http://localhost:8080/bench/a/10

標簽: java
相關文章:
主站蜘蛛池模板: 日本高清色本在线www游戏 | 暖暖在线精品日本中文 | 九九精品视频在线播放 | 日日a.v拍夜夜添久久免费 | 日本韩国台湾香港三级 | 国产精品日韩欧美在线第3页 | 国产精品亚洲片在线观看不卡 | 亚洲成a人片 | 手机亚洲第一页 | 亚洲欧美日本综合 | 久久国产精品久久 | 成人午夜两性视频免费看 | 亚洲欧美一区二区三区久久 | 欧美日韩亚洲综合另类ac | 成年人在线观看网站 | 91亚洲自偷手机在线观看 | 亚洲特一级毛片 | 伊人一级 | 一级做性色a爰片久久毛片 一级做性色a爰片久久毛片免费 | 拍真实国产伦偷精品 | 亚洲国产精品综合久久20 | 国产成人香蕉在线视频网站 | 免费高清国产 | 干综合网 | 久久精品国产亚洲7777小说 | 中文字幕在线一区二区在线 | 久草手机在线观看视频 | 手机看片国产免费久久网 | 中文字幕一区中文亚洲 | 美女在线网站免费的 | 91亚洲精品一区二区福利 | 欧美视频精品在线观看 | 国产精品免费视频能看 | 香蕉福利久久福利久久香蕉 | 美女的让男人桶到爽软件 | 九色愉拍自拍 | 免费的成人a视频在线观看 免费的毛片 | 国产精自产拍久久久久久蜜 | 国产成人久久一区二区三区 | 中文字幕在线看 | 亚洲经典在线中文字幕 |