Lock锁相关以及AQS

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模板方法

猜你喜欢

转载自blog.csdn.net/Yooneep/article/details/88548477