|
@@ -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函数可以结束了!");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|