JAVA里的锁之二AQS使用示例

独占锁的示例

/**
 * 实现一个独占锁
 */
public class Mutex implements Lock {

    private static class Sync extends AbstractQueuedSynchronizer{
        //是否是独占
        protected boolean isHeldExclusively(){
            return getState() == 1;
        }

        //独占式只支持1个acquires
        public boolean tryAcquire(int acquires){
            if(compareAndSetState(0,1)){
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            return false;
        }

        //释放锁
        protected boolean tryRelease(int release){
            if(getState() == 0){
                throw new IllegalMonitorStateException();
            }
            setExclusiveOwnerThread(null);
            setState(0);
            return true;
        }

        Condition newCondition(){
            return new ConditionObject();
        }

    }

    private final Sync sync = new Sync();

    @Override
    public void lock() {
        sync.acquire(1);
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        sync.acquireInterruptibly(1);
    }

    @Override
    public boolean tryLock() {
        return sync.tryAcquire(1);
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return sync.tryAcquireNanos(1,unit.toNanos(time));
    }

    @Override
    public void unlock() {
        sync.release(1);
    }

    @Override
    public Condition newCondition() {
        return sync.newCondition();
    }

    //是否是独占锁
    public boolean isLock(){
        return sync.isHeldExclusively();
    }
    public boolean hasQueuedThreads(){
        return sync.hasQueuedThreads();
    }
}

示例中通过实现Lock接口,然后使用一个内部类继承AbstractQueuedSynchronizer来实现一个独占锁。

共享锁示例

/**
 * 共享锁示例
 */
public class SharedLockInstance implements Lock {

    private final Sync sync = new Sync(2);

    public LockInstance() throws IllegalAccessException {
    }

    //通过匿名内部类可以保证Sync单例
    private static final class Sync extends AbstractQueuedSynchronizer{
        Sync(int state) throws IllegalAccessException {
            if(state <= 0){
                throw new IllegalAccessException("count must large than 0");
            }
            setState(state);
        }

        @Override
        public int tryAcquireShared(int arg){
            for(;;){
                System.out.println("try acquire...");
                int current = getState();
                int now = current - arg;
                if(now < 0 || compareAndSetState(current,now)){
                    return now;
                }
            }
        }

        @Override
        public boolean tryReleaseShared(int arg){
            for(;;){
                System.out.println("try release...");
                int current = getState();
                int now = current + arg;
                if(compareAndSetState(current,now)){
                    return true;
                }
            }
        }

        Condition newCondition(){
            return new ConditionObject();
        }
    }

    @Override
    public void lock() {
        sync.acquireShared(1);
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        sync.acquireInterruptibly(1);
    }

    @Override
    public boolean tryLock() {

        return sync.tryAcquireShared(1) >= 0;
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return sync.tryAcquireSharedNanos(1,unit.toNanos(time));
    }

    @Override
    public void unlock() {
        sync.tryReleaseShared(1);
    }

    @Override
    public Condition newCondition() {
        return sync.newCondition();
    }
}

不论是独占式锁还是共享锁都是在通过继承了AQS里的模板方法,这些模板方法大致分3类:独占式获取与释放同步状态,共享式获取与释放同步状态和查询同步队列中的等待线程情况。

你可能感兴趣的:(java)