国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java無(wú)鎖并發(fā)

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

問(wèn)題描述

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

@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; }}

問(wèn)題解答

回答1:

1.首先,明確兩個(gè)問(wèn)題,synchronized 一般不是跟AtomicXX類(lèi)進(jìn)行比較,更多的是跟ReentrantLock這個(gè)類(lèi)進(jìn)行比較,網(wǎng)上關(guān)于這2者的比較很多,可以自行g(shù)oogle之。

2.問(wèn)題中關(guān)于無(wú)鎖跟有鎖的疑問(wèn),測(cè)試代碼b中的代碼是有問(wèn)題的,

對(duì)于方法a,synchronized代碼塊來(lái)說(shuō),鎖被第一個(gè)進(jìn)來(lái)的線(xiàn)程持有后,后續(xù)線(xiàn)程請(qǐng)求獲取鎖會(huì)被阻塞掛起,直到前面一個(gè)線(xiàn)程釋放鎖,后續(xù)的線(xiàn)程會(huì)恢復(fù)執(zhí)行,由于鎖的存在,20個(gè)請(qǐng)求類(lèi)似于順序執(zhí)行,這一層由jvm調(diào)度

對(duì)于方法b,cas操作是非阻塞的,方法中的while循環(huán)其實(shí)是一直在執(zhí)行(不斷嘗試進(jìn)行cas操作),而我們知道,死循環(huán)是會(huì)消耗cpu資源的,并發(fā)數(shù)越多,線(xiàn)程越多,此處的cas操作越多,必然導(dǎo)致cpu使用率飆升,方法b中的代碼由jmeter測(cè)試的時(shí)候理論上來(lái)說(shuō)應(yīng)該一直由20個(gè)活躍的工作線(xiàn)程存在,cpu與線(xiàn)程模型是另外一個(gè)話(huà)題,線(xiàn)程數(shù)的調(diào)優(yōu)是jvm一個(gè)比較高級(jí)的話(huà)題,感興趣可以自行g(shù)oogle之

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

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實(shí)現(xiàn)的,絕大多少情況下ReentrantLock比synchronized高效

juc(java.util.concurrent)中的核心類(lèi)Aqs(AbstractQueuedSynchronizer)是一個(gè)基于隊(duì)列的 并發(fā)包,默認(rèn)線(xiàn)程在鎖競(jìng)爭(zhēng)(自旋)超過(guò)1000納秒的時(shí)候會(huì)被park(掛起操作),從而減少cpu頻繁的線(xiàn)程切換,可以嘗試調(diào)整方法c中的sleep的時(shí)間參數(shù)。

測(cè)試方法,本機(jī)沒(méi)有裝jmeter,用apache ab做的測(cè)試,測(cè)試命令:

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

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 凯里市| 罗城| 化德县| 筠连县| 金门县| 河北区| 晋宁县| 临高县| 门头沟区| 区。| 岐山县| 弋阳县| 读书| 福州市| 台南市| 林周县| 怀安县| 巴青县| 昌宁县| 应城市| 丽江市| 嘉义市| 基隆市| 平乐县| 上杭县| 宜章县| 柳州市| 乌恰县| 天水市| 长海县| 定远县| 博湖县| 额尔古纳市| 中山市| 皮山县| 凤阳县| 那坡县| 察雅县| 建昌县| 彰化市| 腾冲县|