![AQS.png](https://images2015.cnblogs.com/blog/731047/201605/731047-20160512151435280-1407511167.png)
一个简单的示例:
package net.jcip.examples;
import java.util.concurrent.locks.*;
import net.jcip.annotations.*;
/**
* OneShotLatch
* <p/>
* Binary latch using AbstractQueuedSynchronizer
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class OneShotLatch {
private final Sync sync = new Sync(); //基于委托的方式
public void signal() {
sync.releaseShared(0); //
//会调用
tryAcquireShared}
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(0); //会调用
tryAcquireShared}
private class Sync extends AbstractQueuedSynchronizer { //内部私有类
protected int tryAcquireShared(int ignored) {
// Succeed if latch is open (state == 1), else fail
return (getState() == 1) ? 1 : -1;
}
protected boolean tryReleaseShared(int ignored) {
setState(1); // Latch is now open
return true; // Other threads may now be able to acquire
}
}
}
附件列表
转载请标明出处:http://www.cnblogs.com/ssslinppp/