Callable r = () -> {
try {
log.debug("TryLock");
readLock.tryLock(5, TimeUnit.SECONDS);
Thread.sleep(1000);
log.debug("Read");
readLock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
};
Callable w = () -> {
try {
log.debug("TryLock");
writeLock.tryLock(5, TimeUnit.SECONDS);
Thread.sleep(1000);
log.debug("Write");
writeLock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
};
ExecutorService executor = Executors.newCachedThreadPool();
// executor.submit(w);
// executor.submit(r);
// executor.submit(r);
// executor.shutdown();
// try {
// executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
// } catch (InterruptedException e) {
// log.error(e.getMessage(),e);
// }
executor.invokeAll(Arrays.<Callable<Void>>asList(w,r,r));
Created
May 27, 2016 08:42
-
-
Save didyhu/93c8dfd89f0b3ed8503997ba9c394e52 to your computer and use it in GitHub Desktop.
Java Executors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment