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

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

 前言

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

[[286297]]

First Blood

线上总是出现: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); 
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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

正确处理姿势:

 

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

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

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

 

try{ 
   UserBindInfoMapper.insertIntoDB(userBindInfo); 
}catch(DuplicateKeyException ex){ 
    UserBindInfoMapper.update(userBindInfo); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

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(); 
                } 
             }); 
        } 
  
     } 
  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

可以看到有异常抛出

 

 

 

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

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

 

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

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

Trible Kill

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

 

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

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

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

客户端执行以上的命令:

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

Quadra Kill

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

 

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

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

 

  public static void main(String[] args)  { 
          for (int i = 0; i < 1000; i++) { 
              testConcurrentMap(); 
          } 
      } 
      private static void testConcurrentMap() { 
          final Map<String, Integercount = 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(); 
        } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

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

 

 

 

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

 

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

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

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

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

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

 

  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<String, AtomicInteger> 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(); 
         } 
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

 

 

 

Penta Kill

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

错误示范一

 

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

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

 

 

 

 

1.线程A加现金

2.线程B加现金

3.线程A删除票标志

4.线程B删除票标志

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

错误示范2

 

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

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

正确处理方案

 

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

 

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

2024-10-29 09:42:50

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

2023-06-13 11:11:14

2015-08-26 10:00:31

独立游戏cp错误

2022-05-17 09:32:24

Bash编程Linux

2011-05-31 15:38:37

CSS

2024-12-30 08:29:05

2011-07-30 13:01:23

2023-01-05 13:36:41

Script优化任务

2021-02-26 13:50:37

Java并发代码
点赞
收藏

51CTO技术栈公众号