yegang 2 gadi atpakaļ
vecāks
revīzija
2330bb1a50

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

@@ -87,7 +87,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
 //            while (!connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid), redisTemplate.getValueSerializer().serialize(1))) {
 //                Thread.sleep(3);
 //            }
-            if(redisTemplate.opsForValue().setIfAbsent("mask-setnx-mq-" + mid, Thread.currentThread().getName(), 1000, TimeUnit.MILLISECONDS)) {
+            if (redisTemplate.opsForValue().setIfAbsent("mask-setnx-mq-" + mid, Thread.currentThread().getName(), 1000, TimeUnit.MILLISECONDS)) {
                 Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
                 System.out.println("库存扣减前:" + remaining + "---" + Thread.currentThread());
                 if (remaining.compareTo(amount) >= 0) {
@@ -100,7 +100,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
                 connection.del(key);
                 connection.close();
                 return flag;
-            }else {
+            } else {
                 return flag;
             }
 
@@ -159,22 +159,40 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
     public void doDecrAndCreateOrder(Integer uid, Integer maskId) {
         RLock lock = redissonClient.getLock("mask-" + maskId);
         String threadName = Thread.currentThread().getName();
-
+        log.info("线程:{} 获取锁", threadName);
+        lock.lock();
         try {
-            while (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
-                log.info("线程:{} 获取锁", threadName);
-                Thread.sleep(3000);
-                log.info("doSnatch方法执行结果:{}", this.doSnatch(uid, maskId));
-                if (lock.isLocked()) {
-                    if (lock.isHeldByCurrentThread()) {
-                        lock.unlock();
-                    }
-                }
-                log.info("{}:业务执行完成", threadName);
-                break;
+            Thread.sleep(3000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        log.info("doSnatch方法执行结果:{}", this.doSnatch(uid, maskId));
+        if (lock.isLocked()) {
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
             }
-        } catch(InterruptedException e){
-                e.printStackTrace();
         }
+        log.info("{}:业务执行完成", threadName);
+        if (lock.isLocked()) {
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
+            }
+        }
+//        try {
+//            while (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
+//                log.info("线程:{} 获取锁", threadName);
+//                Thread.sleep(3000);
+//                log.info("doSnatch方法执行结果:{}", this.doSnatch(uid, maskId));
+//                if (lock.isLocked()) {
+//                    if (lock.isHeldByCurrentThread()) {
+//                        lock.unlock();
+//                    }
+//                }
+//                log.info("{}:业务执行完成", threadName);
+//                break;
+//            }
+//        } catch(InterruptedException e){
+//                e.printStackTrace();
+//        }
     }
 }