Bladeren bron

RedissonTest

yegang 2 jaren geleden
bovenliggende
commit
cf7c40509e
3 gewijzigde bestanden met toevoegingen van 120 en 1 verwijderingen
  1. 11 0
      pom.xml
  2. 1 1
      src/main/resources/application.yml
  3. 108 0
      src/test/java/com/sw/RedissonTest.java

+ 11 - 0
pom.xml

@@ -67,6 +67,17 @@
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
+        <!-- redisson -->
+        <dependency>
+            <groupId>org.redisson</groupId>
+            <artifactId>redisson</artifactId>
+            <version>3.16.7</version>
+        </dependency>
+        <dependency>
+            <groupId>org.redisson</groupId>
+            <artifactId>redisson-spring-boot-starter</artifactId>
+            <version>3.16.7</version>
+        </dependency>
         <!--hutool工具类-->
 <!--        <dependency>-->
 <!--            <groupId>cn.hutool</groupId>-->

+ 1 - 1
src/main/resources/application.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8080
+  port: 8089
 spring:
   application:
     name: mask

+ 108 - 0
src/test/java/com/sw/RedissonTest.java

@@ -0,0 +1,108 @@
+package com.sw;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.redisson.Redisson;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author yegang
+ * @create 2022-02-23 10:40
+ **/
+@Slf4j
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class RedissonTest {
+    @Autowired
+    private RedissonClient redissonClient;
+    private CountDownLatch count = new CountDownLatch(2);
+    @Test
+    public void  lock(){
+        RLock lock =   redissonClient.getLock("mask-" + 1);
+        new Thread(()->{
+            String threadName = Thread.currentThread().getName();
+            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            lock.lock(4000, TimeUnit.MILLISECONDS);
+            try {
+                Thread.sleep(3000);
+                log.info("{}:业务执行完成",threadName);
+                count.countDown();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }finally {
+                lock.unlock();
+                log.info("线程:{},释放了锁",threadName);
+            }
+        }).start();
+        new Thread(()->{
+            String threadName = Thread.currentThread().getName();
+            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            lock.lock(4000, TimeUnit.MILLISECONDS);
+            count.countDown();
+            try {
+                Thread.sleep(3000);
+                log.info("{}:业务执行完成",threadName);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }finally {
+                lock.unlock();
+                log.info("线程:{},释放了锁",threadName);
+            }
+        }).start();
+        try {
+            count.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        log.info("子线程都已执行完毕,main函数可以结束了!");
+    }
+    @Test
+    public void  trylock(){
+        RLock lock =   redissonClient.getLock("mask-" + 1);
+        new Thread(()->{
+            String threadName = Thread.currentThread().getName();
+            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            try {
+                lock.tryLock(4000,TimeUnit.MILLISECONDS);
+                Thread.sleep(3000);
+                log.info("{}:业务执行完成",threadName);
+                count.countDown();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            } finally {
+                lock.unlock();
+                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();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            } finally {
+                lock.unlock();
+                log.info("线程:{},释放了锁",threadName);
+            }
+        }).start();
+        try {
+            count.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        log.info("子线程都已执行完毕,main函数可以结束了!");
+    }
+
+}