Lock鎖相關以及AQS

mic_saber發表於2019-03-14

Lock鎖的使用

    Lock lock = new ReentrantLock();
    lock.lock();
    try {
	 // TODO
    } finally {
	 lock.unlock();
    }

這邊鎖的核心在於AQS(AbstractQueuedSynchronizer抽象佇列同步器)

AQS基本方法

private volatile int state;

public int getState() {
	return state;
}

public void setState(int state) {
	this.state = state;
}

protected boolean tryAcquire(int arg) {
	throw new UnsupportedOperationException();
}

protected boolean tryRelease(int arg) {
	throw new UnsupportedOperationException();
}

protected int tryAcquireShared(int arg) {
	throw new UnsupportedOperationException();
}

protected boolean tryReleaseShared(int arg) {
	throw new UnsupportedOperationException();
}

protected boolean isHeldExclusively() {
	throw new UnsupportedOperationException();
}

AQS模板方法

 

相關文章