AQS
### 实现AQS的方法可以实现对应的锁
Semaphore
public class Semaphore_Demo {
// static KaneSemaphore sp =new KaneSemaphore(6);
static Semaphore=new Semaphore(6);
public static void main(String[] args) {
for (int i = 0; i <1000 ; i++) {
new Thread(()->{
sp.acquire(); //就是拿信号量
System.out.println("当前信号量限制....");
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行完了");
sp.release(); //执行完了,就释放信号量
}).start();
}
}
}
public class KaneSemaphore {
private Sync sync;
public KaneSemaphore(int permits) {
sync = new Sync(permits);
}
public void acquire(){
sync.acquireShared(1);
}
public void release(){
sync.releaseShared(1);
}
class Sync extends AbstractQueuedSynchronizer{
private int permits;
public Sync(int permits) {
this.permits = permits;
}
@Override
protected int tryAcquireShared(int arg) {
//定义自己的方法
int state = getState();
int nextState = state + arg;
if(nextState <= permits){
if(compareAndSetState(state,nextState))
return 1;
}
return -1;
}
@Override
protected boolean tryReleaseShared(int arg) {
int state = getState();
if(compareAndSetState(state,state-arg)){
return true;
}else{
return false;
}
}
}
}
CountDownLatch
多个任务成功之后 一起执行
public class CountDownLatch_Demo {
public static void main(String[] args) throws InterruptedException {
KaneCountDownLatch latch = new KaneCountDownLatch(6); //计数为6
for (int i = 0; i <6 ; i++) {
new Thread(()->{
System.out.println("开始准备.....");
latch.countDown();//计数减一
}).start();
Thread.sleep(1000);
}
latch.await(); //每个线程执行一次,则-1,在latch为0的时候开始向下运行 这是这些线程都准备就绪,然后去一起干同一件事
//还有一种方式, 将一个活分为多段,每个线程去干一段
// for (int i = 0; i <6 ; i++) {
// new Thread(()->{
// latch.countDown(); // 计数减一
// try {
// latch.await(); // 阻塞 -- > 0
// System.out.println("线程:"+Thread.currentThread().getName()+"执行完毕");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }).start();
// }
System.out.println("开始干活....");
}
}
public class KaneCountDownLatch {
private Sync sync;
public KaneCountDownLatch(int count) {
sync = new Sync(count);
}
public void countDown(){
sync.releaseShared(1);
}
public void await(){
sync.acquireShared(1);
}
class Sync extends AbstractQueuedSynchronizer{
public Sync(int count) {
setState(count);
}
@Override
protected int tryAcquireShared(int arg) {
return getState()==0?1:-1;
}
@Override
protected boolean tryReleaseShared(int arg) {
while (true){
int c = getState();
if(c==0)
return false;
int nextc = c -1;
if(compareAndSetState(c,nextc)){
return nextc==0;
}
}
}
}
}
CyclicBarrier
public class CyclicBarrier_Demo {
public static void main(String[] args) throws InterruptedException {
KaneCyclicBarrier barrier = new KaneCyclicBarrier(4);
for (int i = 0; i <100 ; i++) { //假设有100个任务,每次只能有固定数量的线程去执行,可以使用这个
new Thread(()->{
barrier.await();
System.out.println("任务开始执行");
}).start();
Thread.sleep(500L);
}
}
}
public class KaneCyclicBarrier {
private final ReentrantLock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
private int count =0; //批次
private final int parties; //多少线程准备就绪?
private Object generation = new Object();
public KaneCyclicBarrier(int parties) {
this.parties = parties;
}
public void await(){
final ReentrantLock lock = this.lock;
lock.lock();
try {
final Object g = generation;
int index = ++count;
if (index == parties) {
nextGeneration();
return;
}
while (true) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(g!=generation){
return;
}
}
}finally {
lock.unlock();
}
}
public void nextGeneration(){
condition.signalAll();
count=0;
generation = new Object();
}
}