乐趣区

关于java: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 类:独占式获取与开释同步状态,共享式获取与开释同步状态和查问同步队列中的期待线程状况。

退出移动版