Forráskód Böngészése

while自旋改为if判断

dinglan 2 éve
szülő
commit
377bc7c6c1

+ 11 - 11
src/main/java/com/sw/service/impl/SnatchMaskServiceImpl.java

@@ -82,10 +82,11 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
         RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
         Boolean flag = false;
         try {
-
-            while (!connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq"), redisTemplate.getValueSerializer().serialize(1))) {
-                Thread.sleep(3);
-            }
+//
+//            while (!connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid), redisTemplate.getValueSerializer().serialize(1))) {
+//                Thread.sleep(3);
+//            }
+            if(connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid), redisTemplate.getValueSerializer().serialize(1))){
                 Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
                 System.out.println("库存扣减前:" + remaining + "---" + Thread.currentThread());
                 if (remaining.compareTo(amount) >= 0) {
@@ -95,19 +96,18 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
                 } else {
                     flag = false;
                 }
+                connection.del(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid));
+                connection.close();
+                return flag;
+            }else {
+                return flag;
+            }
 
-            connection.close();
-            return flag;
-        } catch (InterruptedException e) {
-            e.printStackTrace();
         } finally {
-            connection.del(redisTemplate.getKeySerializer().serialize("mask-setnx-mq"));
             connection.close();
-            return  flag;
         }
 
 
-
 //        reentrantLock.lock();
 //        try {
 //            Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());