首先记下:闭锁和栅栏是通过线程数+await()方法实现的,而闭锁有一个countDown()方法来减线程数;栅栏是可以循环使用的
信号量是通过许可证数+acquire()+release()方法实现的。
1.闭锁场景:通过CountDownLatch的构造方法中的parties,通过每次执行countDown()方法,等计数器减为0后,await()方法不再阻塞,继续往下运行其他内容。(三人同时去吃饭,并行三个线程,最后一起吃饭)
new CountDownLatch(2);
//设置计数器为2
countDownLatch.countDown();
// 计数器减一
countDownLatch.await();
//是否计数器未0,如果是等待其他线程执行完毕
//后在执行main线程里的其他方法;
2.栅栏场景:所有线程互相等待,直到所有线程都到达某一点时才打开栅栏,然后线程继续执行。(学生刷卡后等待,一块放学回家;或者3人一组回家)
Thread.sleep(500); // 执行到这里睡眠500ms
cyclicBarrier.await(); //等待其他的栅栏工作线程 //执行到这里 //才会继续往下执行
3.信号量场景:车库人员管理车辆进出
以下例子是搜集各个地方地方汇集的,有部分修改:以下代码是为了自学,如有侵权可能,请联系我删除
package com.zrtl.tms.today.controller;
import java.util.concurrent.CountDownLatch;
/**
闭锁
* Created by adminstrator on 2020/5/21.
*/
public class Test {
private static CountDownLatch latch = new CountDownLatch(3);
public static void main(String[] args) {
new Thread(){
@Override
public void run(){
System.out.println("father is coming");
latch.countDown();
}
}.start();
new Thread(){
@Override
public void run(){
System.out.println("mother is coming");
latch.countDown();
}
}.start();
new Thread(){
@Override
public void run(){
System.out.println("i am coming");
latch.countDown();
}
}.start();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("all together");
}
}
package com.zrtl.tms.today.controller;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
栅栏
* Created by adminstrator on 2020/5/21.
*/
public class Test3 {
private final int STUDENT_COUNT = 15;
final CyclicBarrier cyclicBarrier = new CyclicBarrier(5,
new Runnable() {
@Override
public void run() {
System.out.println("5个学生可以放行");
}
}
);
private void goHome() throws BrokenBarrierException, InterruptedException {
System.out.println(Thread.currentThread().getName()+"学生已刷卡等待回家");
cyclicBarrier.await();
// System.out.println(Thread.currentThread().getName()+"回家了");
}
public static void main(String[] args) throws InterruptedException {
final Test3 instance = new Test3();
int j = 0;
for(int i=0;i
package com.zrtl.tms.today.controller;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
信号量
* Created by adminstrator on 2020/5/21.
*/
public class TestSemaphore {
private static final Semaphore semaphore = new Semaphore(1);
private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
5,10,60, TimeUnit.SECONDS,new LinkedBlockingDeque()
);
private static class InformationThread extends Thread{
private final String name;
private final int age;
public InformationThread(String name,int age){
this.name = name;
this.age = age;
}
@Override
public void run(){
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName()+":大家好,我是"+name+"我今年"+age+"岁当前时间为:"+System.currentTimeMillis());
Thread.sleep(1000);
System.out.println(name+"要准备释放许可证了,当前时间为:"+System.currentTimeMillis());
semaphore.release();
System.out.println("当前可使用的许可数为:"+semaphore.availablePermits());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
String[] name = {"黎明","王五","张杰","王强","赵二","李四","张三"};
int[] age={26,27,33,45,19,23,41};
for(int i=0;i<7;i++){
Thread t1 = new InformationThread(name[i],age[i]);
threadPool.execute(t1);
}
}
}