How to do it...

Follow these steps to implement the example:

  1. Create a class named MyAbstractQueuedSynchronizer that extends the AbstractQueuedSynchronizer class:
        public class MyAbstractQueuedSynchronizer extends                                        AbstractQueuedSynchronizer {
  1. Declare a private AtomicInteger attribute named state:
        private final AtomicInteger state;
  1. Implement the constructor of the class to initialize its attribute:
        public MyAbstractQueuedSynchronizer() {           state=new AtomicInteger(0);         }
  1. Implement the tryAcquire() method. This method tries to change the value of the state variable from zero to one. If it can, it returns the true value; else, it returns false:
        @Override         protected boolean tryAcquire(int arg) {  return state.compareAndSet(0, 1);  ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.