二、CountDownLatch/CyclicBarrier/Semaphore使用过吗?
https://achang.blog.csdn.net/article/details/117935179
- CountDownLatch倒计时门栓
倒数count为0后,执行await()方法阻塞的线程就发车
所有人出了门,保安才锁门
- CyclicBarrier循环栅栏
满足数量条件,就发车
人到齐了,就开会
- Semaphore信号量
“许可证”,数量有限
- 为什么用?有什么好处?
- 知道哪些实现类吗?
- API
- 手写传统版生产者消费者模式
/******
@author 阿昌
@create 2021-05-22 20:58
- 用wait/notify来实现 生产者消费者模式
*/
public class ProducerConsumerModel {
//主函数
public static void main(String[] args) {
EventStorage eventStorage = new EventStorage();
Consumer1 consumer = new Consumer1(eventStorage);
Producer1 producer = new Producer1(eventStorage);
Thread threadConsumer = new Thread(consumer);
Thread threadProducer = new Thread(producer);
threadConsumer.start();
threadProducer.start();
}
}
//生产者
class Producer1 implements Runnable{
private EventStorage storage;
public Producer1(EventStorage storage) {
this.storage = storage;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
storage.put();
}
}
}
//消费者
class Consumer1 implements Runnable{
private EventStorage storage;
public Consumer1(EventStorage storage) {
this.storage = storage;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
storage.take();
}
}
}
//队列
class EventStorage{
private int maxSize;
private LinkedList storage;
public EventStorage(){
this.maxSize=10;
storage = new LinkedList<>();
}
//生成产品
public synchronized void put(){
//如果满了
while (storage.size()==maxSize){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没满
storage.add(new Date());
System.out.println(“生产者生产了产品,仓库里有了”+storage.size()+“个产品”);
notify();
}
//消费产品
public synchronized void take(){
//如果空了
while (storage.size()<=0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没空
System.out.println(“消费者消费了产品,拿到了”+storage.poll()+“,现在仓库还剩下”+storage.size());
notify();
}
}
- 生产者消费者模式阻塞队列版
public class ConsumerProducer {
static class MySource{
private volatile boolean flag = true;
private BlockingQueue blockingQueue;
private AtomicInteger atomicInteger = new AtomicInteger();
public MySource(BlockingQueue queue)
{
this.blockingQueue = queue;
}
public void prod() throws InterruptedException {
String data = null;
while (flag){
data = atomicInteger.incrementAndGet()+“”;
if (blockingQueue.offer(data,2L, TimeUnit.SECONDS)){
System.out.println(“生产对象,并插入对类”);
}
Thread.sleep(1000);
}
this.flag = false;
System.out.println(“flag=false,停止生产…”);
}
public void consume() throws InterruptedException {
while (flag){
String data = blockingQueue.poll(2L, TimeUnit.SECONDS);
if (data != null || null != “”) {
System.out.println(“消费对象:”+data);
}else {
this.flag = false;
System.out.println(“消费者停止消费”);
}
Thread.sleep(1000);
}
}
public void clear(){
this.flag = false;
}
}
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(10);
MySource mySource = new MySource(queue);
new Thread(new Runnable() {
@Override
public void run() {
try {
mySource.prod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
mySource.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Thread.sleep(3000L);
System.out.println(“主线程停止操作”);
mySource.clear();
}
}
- Synchronized和Lock有什么区别?