Bladeren bron

watch dog

yegang 2 jaren geleden
bovenliggende
commit
f7441c4057
1 gewijzigde bestanden met toevoegingen van 54 en 38 verwijderingen
  1. 54 38
      src/test/java/com/sw/RedissonTest.java

+ 54 - 38
src/test/java/com/sw/RedissonTest.java

@@ -5,6 +5,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.redisson.Redisson;
 import org.redisson.api.RLock;
+import org.redisson.api.RTimeSeries;
 import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -72,31 +73,49 @@ public class RedissonTest {
         new Thread(() -> {
             String threadName = Thread.currentThread().getName();
             log.info("线程:{} 正在尝试获取锁。。。", threadName);
+            boolean b = false;
             try {
-                lock.tryLock(4000, TimeUnit.MILLISECONDS);
-                Thread.sleep(3000);
-                log.info("{}:业务执行完成", threadName);
-                count.countDown();
+                b = lock.tryLock(4000, TimeUnit.MILLISECONDS);
             } catch (InterruptedException e) {
                 e.printStackTrace();
-            } finally {
-                lock.unlock();
-                log.info("线程:{},释放了锁", threadName);
+            }
+            if (b) {
+                try {
+
+                    Thread.sleep(3000);
+                    log.info("{}:业务执行完成", threadName);
+                    count.countDown();
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                } finally {
+                    lock.unlock();
+                    log.info("线程:{},释放了锁", threadName);
+                }
+            } else {
+                log.info("{}:没有获取到锁,锁已被占用", threadName);
             }
         }).start();
         new Thread(() -> {
             String threadName = Thread.currentThread().getName();
             log.info("线程:{} 正在尝试获取锁。。。", threadName);
             try {
-                lock.tryLock(4000, TimeUnit.MILLISECONDS);
-                Thread.sleep(3000);
-                log.info("{}:业务执行完成", threadName);
-                count.countDown();
+                if (lock.tryLock(4000, TimeUnit.MILLISECONDS)) {
+                    try {
+
+                        Thread.sleep(3000);
+                        log.info("{}:业务执行完成", threadName);
+                        count.countDown();
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    } finally {
+                        lock.unlock();
+                        log.info("线程:{},释放了锁", threadName);
+                    }
+                } else {
+                    log.info("{}:没有获取到锁,锁已被占用", threadName);
+                }
             } catch (InterruptedException e) {
                 e.printStackTrace();
-            } finally {
-                lock.unlock();
-                log.info("线程:{},释放了锁", threadName);
             }
         }).start();
         try {
@@ -109,33 +128,30 @@ public class RedissonTest {
 
     @Test
     public void timeout() {
-        RLock lock = redissonClient.getLock("mask-" + 2);
-        new Thread(() -> {
-            String threadName = Thread.currentThread().getName();
-            log.info("线程:{} 正在尝试获取锁。。。", threadName);
-            boolean b = false;
+
+        RLock lock = redissonClient.getLock("mask-" + 3);
+        String threadName = Thread.currentThread().getName();
+        log.info("线程:{} 正在尝试获取锁。。。", threadName);
+        boolean b = false;
+        try {
+            b = lock.tryLock(4000, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        if (b) {
             try {
-                b = lock.tryLock(3, TimeUnit.SECONDS);
-                System.out.println(b);
+
+                Thread.sleep(5000);
+                log.info("{}:业务执行完成", threadName);
             } catch (InterruptedException e) {
                 e.printStackTrace();
+            } finally {
+                lock.unlock();
+                log.info("线程:{},释放了锁", threadName);
             }
-            if (b) {
-                try {
-
-                    Thread.sleep(6000);
-                    log.info("{}:业务执行完成", threadName);
-                } catch (InterruptedException e) {
-                    e.printStackTrace();
-                } finally {
-                    lock.unlock();
-                    log.info("线程:{},释放了锁", threadName);
-                }
-            } else {
-                log.info("{}:没有获取到锁,锁已被占用", threadName);
-            }
-        }).start();
-
-    }
+        } else {
+            log.info("{}:没有获取到锁,锁已被占用", threadName);
+        }
+}
 
 }