日常工作中很容易犯的几个并发错误

开发 前端
列举大家平时在工作中最容易犯的几个并发错误,都是在实际项目代码中看到的鲜活例子,希望对大家有帮助。

 前言

列举大家平时在工作中很容易犯的几个并发错误,都是在实际项目代码中看到的鲜活例子,希望对大家有帮助。

[[286297]]

First Blood

线上总是出现:ERROR 1062 (23000) Duplicate entry 'xxx' for key 'yyy',我们来看一下有问题的这段代码:

  1. UserBindInfo info = selectFromDB(userId); 
  2. if(info == null){ 
  3.     info = new UserBindInfo(userId,deviceId); 
  4.     insertIntoDB(info); 
  5. }else
  6.     info.setDeviceId(deviceId); 
  7.     updateDB(info); 
  8.     } 

在并发情况下,第一步判断都为空,就会有2个或者多个线程进入插入数据库操作,这时候就出现了同一个ID插入多次。

正确处理姿势:

 

  1. insert into UserBindInfo values(#{userId},#{deviceId}) on duplicate key update deviceId=#{deviceId}多次的情况,导致插入失败。 

一般情况下,可以用insert...on duplicate key update... 解决这个问题。

注意: 如果UserBindInfo表存在主键以及一个以上的唯一索引,在并发情况下,使用insert...on duplicate key,可能会产生死锁(Mysql5.7),可以这样处理:

 

  1. try{ 
  2.    UserBindInfoMapper.insertIntoDB(userBindInfo); 
  3. }catch(DuplicateKeyException ex){ 
  4.     UserBindInfoMapper.update(userBindInfo); 

Double Kill

小心你的全局变量,如下面这段代码:

  1.  public class GlobalVariableConcurrentTest { 
  2.    
  3.       private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  4.   
  5.       public static void main(String[] args) throws InterruptedException { 
  6.          ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000)); 
  7.   
  8.          while (true){ 
  9.              threadPoolExecutor.execute(()->{ 
  10.                 String dateString = sdf.format(new Date()); 
  11.                  try { 
  12.                      Date parseDate = sdf.parse(dateString); 
  13.                     String dateString2 = sdf.format(parseDate); 
  14.                      System.out.println(dateString.equals(dateString2)); 
  15.                  } catch (ParseException e) { 
  16.                      e.printStackTrace(); 
  17.                 } 
  18.              }); 
  19.         } 
  20.   
  21.      } 
  22.   

可以看到有异常抛出

 

 

 

全局变量的SimpleDateFormat,在并发情况下,存在安全性问题,阿里Java规约明确要求谨慎使用它。

除了SimpleDateFormat,其实很多时候,面对全局变量,我们都需要考虑并发情况是否存在问题,如下

 

  1.   @Component 
  2.   public class Test { 
  3.   
  4.      public static List<String> desc = new ArrayList<>(); 
  5.    
  6.       public List<String> getDescByUserType(int userType) { 
  7.           if (userType == 1) { 
  8.               desc.add("普通会员不可以发送和查看邮件,请购买会员"); 
  9.               return desc
  10.          } else if (userType == 2) { 
  11.              desc.add("恭喜你已经是VIP会员,尽情的发邮件吧"); 
  12.             return desc
  13.          }else { 
  14.              desc.add("你的身份未知"); 
  15.              return desc
  16.          } 
  17.      } 
  18.  } 

因为desc是全局变量,在并发情况下,请求getDescByUserType方法,得到的可能并不是你想要的结果。

Trible Kill

假设现在有如下业务:控制同一个用户访问某个接口的频率不能小于5秒。一般很容易想到使用redis的 setnx操作来控制并发访问,于是有以下代码:

 

  1.  if(RedisOperation.setnx(userId, 1)){ 
  2.      RedisOperation.expire(userId,5,TimeUnit.SECONDS)); 
  3.       //执行正常业务逻辑 
  4.  }else
  5.       return “访问过于频繁”; 
  6.  } 

假设执行完setnx操作,还没来得及设置expireTime,机器重启或者突然崩溃,将会发生死锁。该用户id,后面执行setnx永远将为false,这可能让你永远损失那个用户。

那么怎么解决这个问题呢,可以考虑用SET key value NX EX max-lock-time ,它是一种在 Redis 中实现锁的方法,是原子性操作,不会像以上代码分两步执行,先set再expire,它是一步到位。

客户端执行以上的命令:

  • 如果服务器返回 OK ,那么这个客户端获得锁。
  • 如果服务器返回 NIL ,那么客户端获取锁失败,可以在稍后再重试。
  • 设置的过期时间到达之后,锁将自动释放

Quadra Kill

我们看一下有关ConcurrentHashMap的一段代码,如下:

 

  1.   //全局变量 
  2.   Map<String, Integer> map = new ConcurrentHashMap();  
  3.   
  4.  Integer value = count.get(k); 
  5.  if(value == null){ 
  6.          map.put(k,1); 
  7.   }else
  8.       map.put(k,value+1); 
  9.   } 

假设两条线程都进入 value==null,这一步,得出的结果是不是会变小?OK,客官先稍作休息,闭目养神一会,我们验证一下,请看一个demo:

 

  1.   public static void main(String[] args)  { 
  2.           for (int i = 0; i < 1000; i++) { 
  3.               testConcurrentMap(); 
  4.           } 
  5.       } 
  6.       private static void testConcurrentMap() { 
  7.           final Map<String, Integercount = new ConcurrentHashMap<>(); 
  8.           ExecutorService executorService = Executors.newFixedThreadPool(2); 
  9.          final CountDownLatch endLatch = new CountDownLatch(2); 
  10.          Runnable task = ()->  { 
  11.                  for (int i = 0; i < 5; i++) { 
  12.                      Integer value = count.get("k"); 
  13.                      if (null == value) { 
  14.                          System.out.println(Thread.currentThread().getName()); 
  15.                          count.put("k", 1); 
  16.                      } else { 
  17.                          count.put("k", value + 1); 
  18.                      } 
  19.                  } 
  20.                  endLatch.countDown(); 
  21.          }; 
  22.   
  23.          executorService.execute(task); 
  24.          executorService.execute(task); 
  25.  
  26.         try { 
  27.              endLatch.await(); 
  28.              if (count.get("k") < 10) { 
  29.                  System.out.println(count); 
  30.              } 
  31.         } catch (Exception e) { 
  32.             e.printStackTrace(); 
  33.         } 

表面看,运行结果应该都是10对吧,好的,我们再看运行结果:

 

 

 

运行结果出现了5,所以这样实现是有并发问题的,那么正确的实现姿势是啥呢?

 

  1.   Map<K,V> map = new ConcurrentHashMap();  
  2.   V v = map.get(k); 
  3.   if(v == null){ 
  4.           V v = new V(); 
  5.           V old = map. putIfAbsent(k,v); 
  6.           if(old != null){ 
  7.                    v = old; 
  8.           } 
  9.   } 

可以考虑使用putIfAbsent解决这个问题

(1)如果key是新的记录,那么会向map中添加该键值对,并返回null。

(2)如果key已经存在,那么不会覆盖已有的值,返回已经存在的值

我们再来看看以下代码以及运行结果:

 

  1.   public static void main(String[] args)  { 
  2.           for (int i = 0; i < 1000; i++) { 
  3.               testConcurrentMap(); 
  4.           } 
  5.       } 
  6.    
  7.       private static void testConcurrentMap() { 
  8.           ExecutorService executorService = Executors.newFixedThreadPool(2); 
  9.           final Map<String, AtomicInteger> map = Maps.newConcurrentMap(); 
  10.          final CountDownLatch countDownLatch = new CountDownLatch(2); 
  11.   
  12.          Runnable task = ()->  { 
  13.                  AtomicInteger oldValue; 
  14.                  for (int i = 0; i < 5; i++) { 
  15.                      oldValue = map.get("k"); 
  16.                     if (null == oldValue) { 
  17.                          AtomicInteger initValue = new AtomicInteger(0); 
  18.                          oldValue = map.putIfAbsent("k", initValue); 
  19.                          if (oldValue == null) { 
  20.                              oldValue = initValue; 
  21.                          } 
  22.                      } 
  23.                      oldValue.incrementAndGet(); 
  24.                  } 
  25.              countDownLatch.countDown(); 
  26.          }; 
  27.   
  28.          executorService.execute(task); 
  29.          executorService.execute(task); 
  30.   
  31.          try { 
  32.              countDownLatch.await(); 
  33.              System.out.println(map); 
  34.          } catch (Exception e) { 
  35.              e.printStackTrace(); 
  36.       } 
  37.     }

 

 

 

Penta Kill

现有如下业务场景:用户手上有一张现金券,可以兑换相应的现金,

错误示范一

 

  1. if(isAvailable(ticketId){ 
  2.     1、给现金增加操作 
  3.     2、deleteTicketById(ticketId) 
  4. }else
  5.     return “没有可用现金券” 

解析: 假设有两条线程A,B兑换现金,执行顺序如下:

 

 

 

 

1.线程A加现金

2.线程B加现金

3.线程A删除票标志

4.线程B删除票标志

显然,这样有问题了,已经给用户加了两次现金了。

错误示范2

 

  1.   if(isAvailable(ticketId){ 
  2.       1、deleteTicketById(ticketId) 
  3.       2、给现金增加操作 
  4.   }else
  5.      return “没有可用现金券” 
  6.   } 

并发情况下,如果一条线程,第一步deleteTicketById删除失败了,也会多添加现金。

正确处理方案

 

  1.   if(deleteAvailableTicketById(ticketId) == 1){ 
  2.       1、给现金增加操作 
  3.   }else
  4.       return “没有可用现金券” 
  5.   } 

 

责任编辑:华轩 来源: 捡田螺的小男孩
相关推荐

2021-06-15 06:04:42

MySQL数据库索引

2022-07-14 07:12:09

PythonPandasVBA

2017-10-18 13:28:27

语言Python开发错误

2013-06-28 11:08:07

运维DBASA

2022-12-26 11:02:06

云计算CIOIT

2016-11-02 12:56:58

Linux新手错误

2019-11-01 14:19:02

大数据机器学习工具

2009-03-27 10:25:24

OracleDBA职责

2020-07-15 07:53:41

VSCode Task脚本命令

2022-06-28 10:13:09

Pandas错误Python

2021-06-27 06:25:14

代码优化技巧Java

2011-05-31 15:38:37

CSS

2022-05-17 09:32:24

Bash编程Linux

2023-06-13 11:11:14

2015-08-26 10:00:31

独立游戏cp错误

2021-02-26 13:50:37

Java并发代码

2019-04-24 08:56:34

Java开发人员常犯错误

2011-04-18 12:55:04

JavaScript开发者

2023-07-19 11:19:37

AI项目人工智能

2016-11-30 14:15:34

网络布线错误
点赞
收藏

51CTO技术栈公众号