Created
June 5, 2020 15:08
-
-
Save anny0739/b9ad9722d78060cc31eaa29b3c127a5a to your computer and use it in GitHub Desktop.
SyncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject | |
private RedissonClient redissonClient; | |
private Exception exception; | |
private final CountDownLatch latch = new CountDownLatch(2); | |
@Test | |
public void syncTest() { | |
List<Thread> threads = IntStream.range(0, 2) | |
.mapToObj(i -> new Thread1()) | |
.map(Thread::new).collect(Collectors.toList()); | |
threads.forEach(Thread::start); | |
threads.forEach(a -> { | |
try { | |
a.join(); | |
} catch (Exception ex) { | |
throw new RuntimeException(ex); | |
} | |
}); | |
assertThat(exception.getClass()).isEqualTo(IllegalMonitorStateException.class); | |
} | |
private void lock() throws Exception { | |
final String lockName = "test-lock"; | |
RLock rLock = redissonClient.getLock(lockName); | |
if (rLock.tryLock(0, 1000, TimeUnit.MILLISECONDS)) { | |
System.out.println("Do it Something"); | |
} | |
rLock.unlock(); | |
} | |
public class Thread1 implements Runnable { | |
@Override | |
public void run() { | |
try { | |
latch.countDown(); | |
latch.await(); | |
lock(); | |
} catch (Exception ex) { | |
exception = ex; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment