Skip to content

Instantly share code, notes, and snippets.

@didyhu
Created May 27, 2016 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didyhu/93c8dfd89f0b3ed8503997ba9c394e52 to your computer and use it in GitHub Desktop.
Save didyhu/93c8dfd89f0b3ed8503997ba9c394e52 to your computer and use it in GitHub Desktop.
Java Executors
        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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment