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