Skip to content

Instantly share code, notes, and snippets.

@anny0739
Created June 5, 2020 15:08
Show Gist options
  • Save anny0739/b9ad9722d78060cc31eaa29b3c127a5a to your computer and use it in GitHub Desktop.
Save anny0739/b9ad9722d78060cc31eaa29b3c127a5a to your computer and use it in GitHub Desktop.
SyncTest.java
@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