Follow these steps to implement the example:
- Create a class named MyAbstractQueuedSynchronizer that extends the AbstractQueuedSynchronizer class:
public class MyAbstractQueuedSynchronizer extends AbstractQueuedSynchronizer {
- Declare a private AtomicInteger attribute named state:
private final AtomicInteger state;
- Implement the constructor of the class to initialize its attribute:
public MyAbstractQueuedSynchronizer() { state=new AtomicInteger(0); }
- 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); ...