Browse Source

redisson 测试超时2

yegang 2 years ago
parent
commit
2877e05b69
1 changed files with 40 additions and 9 deletions
  1. 40 9
      src/test/java/com/sw/RedissonTest.java

+ 40 - 9
src/test/java/com/sw/RedissonTest.java

@@ -40,7 +40,11 @@ public class RedissonTest {
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } finally {
-                lock.unlock();
+                if(lock.isLocked()){
+                    if(lock.isHeldByCurrentThread()){
+                        lock.unlock();
+                    }
+                }
                 log.info("线程:{},释放了锁", threadName);
             }
         }).start();
@@ -55,7 +59,11 @@ public class RedissonTest {
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } finally {
-                lock.unlock();
+                if(lock.isLocked()){
+                    if(lock.isHeldByCurrentThread()){
+                        lock.unlock();
+                    }
+                }
                 log.info("线程:{},释放了锁", threadName);
             }
         }).start();
@@ -88,7 +96,11 @@ public class RedissonTest {
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 } finally {
-                    lock.unlock();
+                    if(lock.isLocked()){
+                        if(lock.isHeldByCurrentThread()){
+                            lock.unlock();
+                        }
+                    }
                     log.info("线程:{},释放了锁", threadName);
                 }
             } else {
@@ -108,7 +120,11 @@ public class RedissonTest {
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     } finally {
-                        lock.unlock();
+                        if(lock.isLocked()){
+                            if(lock.isHeldByCurrentThread()){
+                                lock.unlock();
+                            }
+                        }
                         log.info("线程:{},释放了锁", threadName);
                     }
                 } else {
@@ -142,14 +158,19 @@ public class RedissonTest {
 
                     Thread.sleep(6000);
                     log.info("{}:业务执行完成", threadName);
-                    count.countDown();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 } finally {
-                    lock.unlock();
+                    if(lock.isLocked()){
+                        if(lock.isHeldByCurrentThread()){
+                            count.countDown();
+                            lock.unlock();
+                        }
+                    }
                     log.info("线程:{},释放了锁", threadName);
                 }
             } else {
+                count.countDown();
                 log.info("{}:没有获取到锁,锁已被占用", threadName);
             }
         }).start();
@@ -162,14 +183,20 @@ public class RedissonTest {
 
                         Thread.sleep(6000);
                         log.info("{}:业务执行完成", threadName);
-                        count.countDown();
+
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     } finally {
-                        lock.unlock();
+                        if(lock.isLocked()){
+                            if(lock.isHeldByCurrentThread()){
+                                count.countDown();
+                                lock.unlock();
+                            }
+                        }
                         log.info("线程:{},释放了锁", threadName);
                     }
                 } else {
+                    count.countDown();
                     log.info("{}:没有获取到锁,锁已被占用", threadName);
                 }
             } catch (InterruptedException e) {
@@ -203,7 +230,11 @@ public class RedissonTest {
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } finally {
-                lock.unlock();
+                if(lock.isLocked()){
+                    if(lock.isHeldByCurrentThread()){
+                        lock.unlock();
+                    }
+                }
                 log.info("线程:{},释放了锁", threadName);
             }
         } else {