1.Condition
- JUC Lock 线程间通信工具类
/**
* Description:ConditionWait
*
* @date 2019-12-12 16:12
*/
public class ConditionWait implements Runnable{
private Lock lock;
private Condition condition;
public ConditionWait(Lock lock, Condition condition) {
this.lock = lock;
this.condition = condition;
}
@Override
public void run() {
lock.lock();//Step1:Wait线程抢占锁成功
try {
System.out.println("begin ConditionWait");//Step2:Wait线程condition.await()
condition.await();//
System.out.println("end ConditionWait");
}catch (Exception e){
e.printStackTrace();
}
finally {
lock.unlock();
}
}
}
/**
* Description:ConditionNotify
*
* @date 2019-12-12 16:25
*/
public class ConditionNotify implements Runnable{
private Lock lock;
private Condition condition;
public ConditionNotify(Lock lock, Condition condition) {
this.lock = lock;
this.condition = condition;
}
@Override
public void run() {
lock.lock();//
try {
System.out.println("begin ConditionNotify");
condition.signal();//Step3:Notify线程condition.signal()
System.out.println("end ConditionNotify");
}catch (Exception e){
e.printStackTrace();
}
finally {
lock.unlock();
}
}
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(new ConditionWait(lock,condition)).start();
new Thread(new ConditionNotify(lock,condition)).start();
}
}
- 执行结果:
begin ConditionWait
begin ConditionNotify
end ConditionNotify
end ConditionWait
-
流程图
2.CountDownLatch
- 倒计时计数器
- 主要方法
.await()
.countDown()
public class CountDownLatchDemo {
public static void main(String[] args) throws Exception{
CountDownLatch countDownLatch = new CountDownLatch(3);//countDownLatch = 3
new Thread(()->{
countDownLatch.countDown();//countDownLatch-=1
}).start();
new Thread(()->{
countDownLatch.countDown();//countDownLatch-=1
}).start();
new Thread(()->{
countDownLatch.countDown();//countDownLatch-=1
}).start();
countDownLatch.await();//阻塞当前线程,直到countDownLatch=0
}
}
3.Semaphore
- ''限流器"
public class SemaphoreDemo implements Runnable {
private int num;
private Semaphore semaphore;
public SemaphoreDemo(int num, Semaphore semaphore) {
this.num = num;
this.semaphore = semaphore;
}
@Override
public void run() {
try {
semaphore.acquire();//获取一个令牌,如果取不到阻塞
System.out.println("第"+num+"个线程 抢占令牌");
Thread.sleep(2000);
System.out.println("第"+num+"个线程 释放令牌");
semaphore.release();//释放令牌
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(10);//设置令牌总数:10
for(int i=0;i<100;i++){
new Thread(new SemaphoreDemo(i,semaphore)).start();
}
}
4.CyclicBarrier
- "循环栅栏"
public class CyclicBarrierDemo implements Runnable {
private int code;
private CyclicBarrier cyclicBarrier;
public CyclicBarrierDemo(int code, CyclicBarrier cyclicBarrier) {
this.code = code;
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
System.out.println("编号:"+code+"的学员已经就位");
try {
cyclicBarrier.await();//阻塞,直到去除屏障
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Thread(()->{
System.out.println("人员到齐,出发");
}));
new Thread( new CyclicBarrierDemo(1,cyclicBarrier)).start();
new Thread( new CyclicBarrierDemo(2,cyclicBarrier)).start();
new Thread( new CyclicBarrierDemo(3,cyclicBarrier)).start();
}