请在请求中携带deviceid参数_日常工作中最容易犯的几个并发错误,你中了几个?...
作者:Jay_huaxiao
前言
列舉大家平時在工作中最容易犯的幾個并發(fā)錯誤,都是在實際項目代碼中看到的鮮活例子,希望對大家有幫助。
First Blood
線上總是出現(xiàn):ERROR 1062 (23000) Duplicate entry 'xxx' for key 'yyy',我們來看一下有問題的這段代碼:
UserBindInfo info = selectFromDB(userId);if(info == null){info = new UserBindInfo(userId,deviceId);insertIntoDB(info);}else{info.setDeviceId(deviceId);updateDB(info);}在并發(fā)情況下,第一步判斷都為空,就會有2個或者多個線程進入插入數(shù)據(jù)庫操作, 這時候就出現(xiàn)了同一個ID插入多次。
正確處理姿勢:
insert into UserBindInfo values(#{userId},#{deviceId}) on duplicate key update deviceId=#{deviceId}多次的情況,導致插入失敗。一般情況下,可以用insert...on duplicate key update... 解決這個問題。
注意: 如果UserBindInfo表存在主鍵以及一個以上的唯一索引,在并發(fā)情況下,使用insert...on duplicate key,可能會產(chǎn)生死鎖(Mysql5.7),可以這樣處理:
try{ UserBindInfoMapper.insertIntoDB(userBindInfo);}catch(DuplicateKeyException ex){ UserBindInfoMapper.update(userBindInfo);}Double Kill
小心你的全局變量,如下面這段代碼:
public class GlobalVariableConcurrentTest { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws InterruptedException { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000)); while (true){ threadPoolExecutor.execute(()->{ String dateString = sdf.format(new Date()); try { Date parseDate = sdf.parse(dateString); String dateString2 = sdf.format(parseDate); System.out.println(dateString.equals(dateString2)); } catch (ParseException e) { e.printStackTrace(); } }); } }}可以看到有異常拋出
全局變量的SimpleDateFormat,在并發(fā)情況下,存在安全性問題,阿里Java規(guī)約明確要求謹慎使用它。
除了SimpleDateFormat,其實很多時候,面對全局變量,我們都需要考慮并發(fā)情況是否存在問題,如下
@Componentpublic class Test { public static List desc = new ArrayList<>(); public List getDescByUserType(int userType) { if (userType == 1) { desc.add("普通會員不可以發(fā)送和查看郵件,請購買會員"); return desc; } else if (userType == 2) { desc.add("恭喜你已經(jīng)是VIP會員,盡情的發(fā)郵件吧"); return desc; }else { desc.add("你的身份未知"); return desc; } }}因為desc是全局變量,在并發(fā)情況下,請求getDescByUserType方法,得到的可能并不是你想要的結(jié)果。
Trible Kill
假設(shè)現(xiàn)在有如下業(yè)務(wù):控制同一個用戶訪問某個接口的頻率不能小于5秒。一般很容易想到使用redis的 setnx操作來控制并發(fā)訪問,于是有以下代碼:
if(RedisOperation.setnx(userId, 1)){RedisOperation.expire(userId,5,TimeUnit.SECONDS));//執(zhí)行正常業(yè)務(wù)邏輯}else{return “訪問過于頻繁”;}假設(shè)執(zhí)行完setnx操作,還沒來得及設(shè)置expireTime,機器重啟或者突然崩潰,將會發(fā)生死鎖。該用戶id,后面執(zhí)行setnx永遠將為false,這可能讓你永遠損失那個用戶。
那么怎么解決這個問題呢,可以考慮用SET key value NX EX max-lock-time ,它是一種在 Redis 中實現(xiàn)鎖的方法,是原子性操作,不會像以上代碼分兩步執(zhí)行,先set再expire,它是一步到位。
客戶端執(zhí)行以上的命令:
- 如果服務(wù)器返回 OK ,那么這個客戶端獲得鎖。
- 如果服務(wù)器返回 NIL ,那么客戶端獲取鎖失敗,可以在稍后再重試。
- 設(shè)置的過期時間到達之后,鎖將自動釋放
Quadra Kill
我們看一下有關(guān)ConcurrentHashMap的一段代碼,如下:
//全局變量Map map = new ConcurrentHashMap(); Integer value = count.get(k);if(value == null){ map.put(k,1);}else{ map.put(k,value+1);}假設(shè)兩條線程都進入 value==null,這一步,得出的結(jié)果是不是會變小?OK,客官先稍作休息,閉目養(yǎng)神一會,我們驗證一下,請看一個demo:
public static void main(String[] args) { for (int i = 0; i < 1000; i++) { testConcurrentMap(); } } private static void testConcurrentMap() { final Map count = new ConcurrentHashMap<>(); ExecutorService executorService = Executors.newFixedThreadPool(2); final CountDownLatch endLatch = new CountDownLatch(2); Runnable task = ()-> { for (int i = 0; i < 5; i++) { Integer value = count.get("k"); if (null == value) { System.out.println(Thread.currentThread().getName()); count.put("k", 1); } else { count.put("k", value + 1); } } endLatch.countDown(); }; executorService.execute(task); executorService.execute(task); try { endLatch.await(); if (count.get("k") < 10) { System.out.println(count); } } catch (Exception e) { e.printStackTrace(); }表面看,運行結(jié)果應(yīng)該都是10對吧,好的,我們再看運行結(jié)果 :
運行結(jié)果出現(xiàn)了5,所以這樣實現(xiàn)是有并發(fā)問題的,那么正確的實現(xiàn)姿勢是啥呢?
Map map = new ConcurrentHashMap(); V v = map.get(k);if(v == null){ v = new V(); V old = map. putIfAbsent(k,v); if(old != null){ v = old; }}可以考慮使用putIfAbsent解決這個問題
(1)如果key是新的記錄,那么會向map中添加該鍵值對,并返回null。
(2)如果key已經(jīng)存在,那么不會覆蓋已有的值,返回已經(jīng)存在的值
我們再來看看以下代碼以及運行結(jié)果:
public static void main(String[] args) { for (int i = 0; i < 1000; i++) { testConcurrentMap(); } } private static void testConcurrentMap() { ExecutorService executorService = Executors.newFixedThreadPool(2); final Map map = Maps.newConcurrentMap(); final CountDownLatch countDownLatch = new CountDownLatch(2); Runnable task = ()-> { AtomicInteger oldValue; for (int i = 0; i < 5; i++) { oldValue = map.get("k"); if (null == oldValue) { AtomicInteger initValue = new AtomicInteger(0); oldValue = map.putIfAbsent("k", initValue); if (oldValue == null) { oldValue = initValue; } } oldValue.incrementAndGet(); } countDownLatch.countDown(); }; executorService.execute(task); executorService.execute(task); try { countDownLatch.await(); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } }Penta Kill
現(xiàn)有如下業(yè)務(wù)場景:用戶手上有一張現(xiàn)金券,可以兌換相應(yīng)的現(xiàn)金,
錯誤示范一
if(isAvailable(ticketId){1、給現(xiàn)金增加操作2、deleteTicketById(ticketId)}else{return “沒有可用現(xiàn)金券”}解析: 假設(shè)有兩條線程A,B兌換現(xiàn)金,執(zhí)行順序如下:
- 1.線程A加現(xiàn)金
- 2.線程B加現(xiàn)金
- 3.線程A刪除票標志
- 4.線程B刪除票標志
顯然,這樣有問題了,已經(jīng)給用戶加了兩次現(xiàn)金了。
錯誤示范2
if(isAvailable(ticketId){1、deleteTicketById(ticketId)2、給現(xiàn)金增加操作}else{return “沒有可用現(xiàn)金券”}并發(fā)情況下,如果一條線程,第一步deleteTicketById刪除失敗了,也會多添加現(xiàn)金。
正確處理方案
if(deleteAvailableTicketById(ticketId) == 1){1、給現(xiàn)金增加操作}else{return “沒有可用現(xiàn)金券”}來源:掘金 鏈接:https://juejin.im/post/5dfec2806fb9a016043a59a6
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的请在请求中携带deviceid参数_日常工作中最容易犯的几个并发错误,你中了几个?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新材料带来的战争技术碾压
- 下一篇: 6艘094核潜艇近乎连续的巡逻