|
@@ -24,37 +24,38 @@ 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(()->{
|
|
|
+ public void lock() {
|
|
|
+ RLock lock = redissonClient.getLock("mask-" + 1);
|
|
|
+ new Thread(() -> {
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
- log.info("线程:{} 正在尝试获取锁。。。",threadName);
|
|
|
+ log.info("线程:{} 正在尝试获取锁。。。", threadName);
|
|
|
lock.lock(4000, TimeUnit.MILLISECONDS);
|
|
|
try {
|
|
|
Thread.sleep(3000);
|
|
|
- log.info("{}:业务执行完成",threadName);
|
|
|
+ log.info("{}:业务执行完成", threadName);
|
|
|
count.countDown();
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
lock.unlock();
|
|
|
- log.info("线程:{},释放了锁",threadName);
|
|
|
+ log.info("线程:{},释放了锁", threadName);
|
|
|
}
|
|
|
}).start();
|
|
|
- new Thread(()->{
|
|
|
+ new Thread(() -> {
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
- log.info("线程:{} 正在尝试获取锁。。。",threadName);
|
|
|
+ log.info("线程:{} 正在尝试获取锁。。。", threadName);
|
|
|
lock.lock(4000, TimeUnit.MILLISECONDS);
|
|
|
count.countDown();
|
|
|
try {
|
|
|
Thread.sleep(3000);
|
|
|
- log.info("{}:业务执行完成",threadName);
|
|
|
+ log.info("{}:业务执行完成", threadName);
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
lock.unlock();
|
|
|
- log.info("线程:{},释放了锁",threadName);
|
|
|
+ log.info("线程:{},释放了锁", threadName);
|
|
|
}
|
|
|
}).start();
|
|
|
try {
|
|
@@ -64,37 +65,38 @@ public class RedissonTest {
|
|
|
}
|
|
|
log.info("子线程都已执行完毕,main函数可以结束了!");
|
|
|
}
|
|
|
+
|
|
|
@Test
|
|
|
- public void trylock(){
|
|
|
- RLock lock = redissonClient.getLock("mask-" + 1);
|
|
|
- new Thread(()->{
|
|
|
+ public void trylock() {
|
|
|
+ RLock lock = redissonClient.getLock("mask-" + 1);
|
|
|
+ new Thread(() -> {
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
- log.info("线程:{} 正在尝试获取锁。。。",threadName);
|
|
|
+ log.info("线程:{} 正在尝试获取锁。。。", threadName);
|
|
|
try {
|
|
|
- lock.tryLock(4000,TimeUnit.MILLISECONDS);
|
|
|
+ lock.tryLock(4000, TimeUnit.MILLISECONDS);
|
|
|
Thread.sleep(3000);
|
|
|
- log.info("{}:业务执行完成",threadName);
|
|
|
+ log.info("{}:业务执行完成", threadName);
|
|
|
count.countDown();
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
lock.unlock();
|
|
|
- log.info("线程:{},释放了锁",threadName);
|
|
|
+ log.info("线程:{},释放了锁", threadName);
|
|
|
}
|
|
|
}).start();
|
|
|
- new Thread(()->{
|
|
|
+ new Thread(() -> {
|
|
|
String threadName = Thread.currentThread().getName();
|
|
|
- log.info("线程:{} 正在尝试获取锁。。。",threadName);
|
|
|
+ log.info("线程:{} 正在尝试获取锁。。。", threadName);
|
|
|
try {
|
|
|
- lock.tryLock(4000,TimeUnit.MILLISECONDS);
|
|
|
+ lock.tryLock(4000, TimeUnit.MILLISECONDS);
|
|
|
Thread.sleep(3000);
|
|
|
- log.info("{}:业务执行完成",threadName);
|
|
|
+ log.info("{}:业务执行完成", threadName);
|
|
|
count.countDown();
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
lock.unlock();
|
|
|
- log.info("线程:{},释放了锁",threadName);
|
|
|
+ log.info("线程:{},释放了锁", threadName);
|
|
|
}
|
|
|
}).start();
|
|
|
try {
|
|
@@ -105,4 +107,35 @@ public class RedissonTest {
|
|
|
log.info("子线程都已执行完毕,main函数可以结束了!");
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void timeout() {
|
|
|
+ RLock lock = redissonClient.getLock("mask-" + 2);
|
|
|
+ new Thread(() -> {
|
|
|
+ String threadName = Thread.currentThread().getName();
|
|
|
+ log.info("线程:{} 正在尝试获取锁。。。", threadName);
|
|
|
+ boolean b = false;
|
|
|
+ try {
|
|
|
+ b = lock.tryLock(3, TimeUnit.SECONDS);
|
|
|
+ System.out.println(b);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|