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