多線程(三)協(xié)作篇之生產(chǎn)者消費(fèi)者模型

使用多個(gè)線程除了并發(fā)進(jìn)行提高一個(gè)大任務(wù)或者多個(gè)子任務(wù)的執(zhí)行效率,多線程之間還存在協(xié)作完成任務(wù)涧偷,共同協(xié)作完成任務(wù)涉及到多線程之間的通訊戳稽。生產(chǎn)者消費(fèi)者模型就是典型的多線程協(xié)作的應(yīng)用


是什么:

什么是線程間的協(xié)作

  • 多線程協(xié)作即多線程之間通過多線程通訊技術(shù)筹陵,實(shí)現(xiàn)多線程按照特定順序共同協(xié)作完成一項(xiàng)任務(wù)益老。

怎么用:

多線程協(xié)作用在哪

  • 多線程協(xié)作場景有多種使用場景,典型的有線程通過通知機(jī)制交替進(jìn)行的生產(chǎn)者消費(fèi)者模型:生產(chǎn)者生產(chǎn)物品,生產(chǎn)完通知消費(fèi)者進(jìn)行消費(fèi)悲立,消費(fèi)者收到通知后進(jìn)行消費(fèi)鹿寨,消費(fèi)完再通知生產(chǎn)者進(jìn)行生產(chǎn);
  • 多個(gè)線程共同等待一齊完成后集體進(jìn)行下一步薪夕,以此循環(huán)共同推進(jìn)的循環(huán)路障模型
  • 某個(gè)線程任務(wù)等待多個(gè)線程都就緒后才開始的倒計(jì)時(shí)門閘模型

用什么實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型

  • 使用objcet的自帶的wait脚草,notify,notifyAll方法進(jìn)行線程間的通知等待機(jī)制原献,需要注意馏慨,這幾個(gè)方法必須在synchronized的同步代碼塊的范圍內(nèi)使用,否則會拋異常姑隅。
public class ProduceConsumeDemo {
    public static void main(String[] args) {
        List<String> container=new ArrayList<>();
        ExecutorService executorService= Executors.newFixedThreadPool(2);
        executorService.submit(new Consumer(container));
        executorService.submit(new Producer(container));
        executorService.shutdown();
    }
}
@Slf4j
class Consumer implements Runnable{
    int money=0;
    private List<String> container;
    public Consumer(List<String> container){
        this.container=container;
    }
    void consumeFood() throws Exception{
        while (money<15){
            synchronized (container){
                log.debug("container owner is consumer,money is {}",money);
                if(container.size()==0){
                    container.wait();
                }
                Iterator it=container.iterator();
                while(it.hasNext()){
                    money++;
                    log.debug("wow,the {} is delicious写隶!nice food!",it.next());
                    it.remove();
                }
                log.debug(" container is empty,it is time to cook!");
                container.notifyAll();
                log.debug("consume  notified continue");
            }
        }
        log.debug("consumer money cost =15,over");
    }

    @Override
    public void run() {
        try {
            consumeFood();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

@Slf4j
class Producer implements Runnable{
    private String[] dishes={"rice","beef","chicken","tomato","potato"};
    int money=0;
    private Random random=new Random();
    private List<String> container;
    public Producer(List<String> container){
        this.container=container;
    }
    void produceFood() throws Exception{
        while (money<15){
            synchronized (container){
                log.debug("container owner is  produce,money is {}",money);
                if(container.size()>=5){
                    container.wait();
                }
                while(container.size()<5){
                    String dish=dishes[random.nextInt(5)];
                    log.debug("new food is cooked,it is {}",dish);
                    container.add(dish);
                    money++;
                }
                log.debug(" container is full,it is time to eat");
                // 做完后喚醒其他
                container.notifyAll();
                log.debug("produce  notified continue");
            }
        }
        log.debug("producer money cost =15,over");
    }

    @Override
    public void run() {
        try {
            produceFood();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

打印如下:
19:54:05.007 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - container owner is consumer,money is 0
19:54:05.011 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - container owner is  produce,money is 0
19:54:05.011 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is rice
19:54:05.011 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is beef
19:54:05.011 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is potato
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is potato
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is tomato
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer -  container is full,it is time to eat
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - produce  notified continue
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - container owner is  produce,money is 5
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the rice is delicious粤策!nice food樟澜!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the beef is delicious误窖!nice food叮盘!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the potato is delicious!nice food霹俺!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the potato is delicious柔吼!nice food!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the tomato is delicious丙唧!nice food愈魏!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer -  container is empty,it is time to cook!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - consume  notified continue
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - container owner is consumer,money is 5
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is tomato
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is chicken
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is chicken
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is tomato
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is rice
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer -  container is full,it is time to eat
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - produce  notified continue
19:54:05.012 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - container owner is  produce,money is 10
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the tomato is delicious!nice food想际!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the chicken is delicious培漏!nice food!
19:54:05.012 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the chicken is delicious胡本!nice food牌柄!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the tomato is delicious!nice food侧甫!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the rice is delicious珊佣!nice food!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer -  container is empty,it is time to cook!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - consume  notified continue
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - container owner is consumer,money is 10
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is tomato
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is beef
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is rice
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is tomato
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - new food is cooked,it is tomato
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer -  container is full,it is time to eat
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - produce  notified continue
19:54:05.013 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.Producer - producer money cost =15,over
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the tomato is delicious披粟!nice food咒锻!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the beef is delicious!nice food守屉!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the rice is delicious惑艇!nice food!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the tomato is delicious拇泛!nice food滨巴!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - wow,the tomato is delicious须板!nice food!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer -  container is empty,it is time to cook!
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - consume  notified continue
19:54:05.013 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.Consumer - consumer money cost =15,over
  • 使用并發(fā)隊(duì)列BlockingQueue兢卵,BlockingQueue本身就是為生產(chǎn)者消費(fèi)者模型設(shè)計(jì)的線程安全的并發(fā)容器习瑰,使用起來簡單高效。
@Slf4j
public class ProduceConsumerQueueDemo {
    public static void main(String[] args) throws Exception{
        BlockingQueue<String> queue=new LinkedBlockingDeque<>(5);
        Random random=new Random();
        List<String> foods= Arrays.asList("beef","nice","meat","tomato","potato");
        ExecutorService executorService= Executors.newCachedThreadPool();
        CountDownLatch countDownLatch=new CountDownLatch(2);
        executorService.execute(()->{
            int i=0;
            String food;
            while(i<15){
                try {
                    food=queue.poll(10L, TimeUnit.SECONDS);
                    log.debug("delicious food秽荤,i like the {},size is {}",food,queue.size());
                    i++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            countDownLatch.countDown();
            log.debug("eat over!");
        });
        executorService.execute(()->{
            int i=0;
            String food;
            while(i<15){
                try {
                    food=foods.get(random.nextInt(5));
                    queue.offer(food,10L, TimeUnit.SECONDS);
                    log.debug("delicious food is cooked {},queue size is {}",food,queue.size());
                    i++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            countDownLatch.countDown();
            log.debug("cook over!");
        });
        countDownLatch.await();
        log.debug("main over!");
        executorService.shutdown();
    }
}
打印結(jié)果如下:
20:24:04.513 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked tomato,queue size is 1
20:24:04.513 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food甜奄,i like the tomato,size is 0
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food,i like the beef,size is 0
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked beef,queue size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked nice,queue size is 1
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food窃款,i like the nice,size is 0
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked beef,queue size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked beef,queue size is 1
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food课兄,i like the beef,size is 0
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked tomato,queue size is 2
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food,i like the beef,size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked beef,queue size is 2
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food晨继,i like the tomato,size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked beef,queue size is 2
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food烟阐,i like the beef,size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked tomato,queue size is 2
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food,i like the beef,size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked nice,queue size is 2
20:24:04.516 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food紊扬,i like the tomato,size is 1
20:24:04.516 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked nice,queue size is 2
20:24:04.517 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked nice,queue size is 2
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food蜒茄,i like the nice,size is 1
20:24:04.517 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked potato,queue size is 3
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food,i like the nice,size is 2
20:24:04.517 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked meat,queue size is 3
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food餐屎,i like the nice,size is 2
20:24:04.517 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food is cooked tomato,queue size is 3
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food檀葛,i like the potato,size is 2
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food,i like the meat,size is 1
20:24:04.517 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - cook over!
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - delicious food腹缩,i like the tomato,size is 0
20:24:04.517 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - eat over!
20:24:04.517 [main] DEBUG com.dz.demo.multiThread.ProduceConsumerQueueDemo - main over!
  • 使用LockSupport相關(guān)park與unpark方法屿聋,park方法使當(dāng)前線程需要一個(gè)令牌才能繼續(xù)(該令牌可以在park之前就被有或者park后被其他線程發(fā)放),unpark方法是給對應(yīng)線程發(fā)放一個(gè)令牌藏鹊。
public class ProduceConsumeLockSupportDemo {
    public static void main(String[] args) throws Exception{
        List<String>container=new ArrayList<>(5);
        Map<String,Thread> threadMap=new ConcurrentHashMap<>();
        ExecutorService executorService= Executors.newFixedThreadPool(2);
        executorService.execute(new LockSupportProducer(threadMap,container));
        executorService.execute(new LockSupportConsumer(threadMap,container));
        executorService.shutdown();
    }
}

@Slf4j
class LockSupportProducer implements Runnable{
    private Map<String,Thread> threadMap;
    private List<String>container;
    public LockSupportProducer(Map<String,Thread> threadMap,List<String>container){
        this.threadMap=threadMap;
        this.container=container;
    }
    private  int money=0;
    private String[] dishes={"rice","beef","chicken","tomato","potato"};
    private Random random=new Random();
    @Override
    public void run() {
        threadMap.put("producer",Thread.currentThread());
        while(money<15){
            if(container.size()<5){
                String food=dishes[random.nextInt(5)];
                log.debug("cooking ,this time food is {},time is {}",food,money);
                container.add(food);
                money++;
            }else {
                LockSupport.unpark(threadMap.get("consumer"));
                LockSupport.park();
            }
        }
        LockSupport.unpark(threadMap.get("consumer"));
        log.debug("cooking over润讥,time is {}",money);
    }
}

@Slf4j
class LockSupportConsumer implements Runnable{

    private Map<String,Thread> threadMap;
    private List<String>container;
    private  int money=0;
    public LockSupportConsumer(Map<String,Thread> threadMap,List<String>container){
        this.threadMap=threadMap;
        this.container=container;
    }
    @Override
    public void run() {
        threadMap.put("consumer",Thread.currentThread());
        while(money<15){
            if(container.size()>0){
                Iterator<String> it=container.iterator();
                while (it.hasNext()){
                    String food=it.next();
                    it.remove();
                    log.debug("eating ,delicious food is {},time is {}",food,money);
                    money++;
                }
            }else {
                LockSupport.unpark(threadMap.get("producer"));
                LockSupport.park();
            }
        }
        log.debug("eating over,time is {}",money);
    }
}
打印結(jié)果如下:
20:30:03.768 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is chicken,time is 0
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is rice,time is 1
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is beef,time is 2
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is beef,time is 3
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is potato,time is 4
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is chicken,time is 0
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is rice,time is 1
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is beef,time is 2
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is beef,time is 3
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is potato,time is 4
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is potato,time is 5
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is chicken,time is 6
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is potato,time is 7
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is potato,time is 8
20:30:03.772 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is chicken,time is 9
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is potato,time is 5
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is chicken,time is 6
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is potato,time is 7
20:30:03.772 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is potato,time is 8
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is chicken,time is 9
20:30:03.773 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is rice,time is 10
20:30:03.773 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is beef,time is 11
20:30:03.773 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is chicken,time is 12
20:30:03.773 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is beef,time is 13
20:30:03.773 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking ,this time food is potato,time is 14
20:30:03.773 [pool-1-thread-1] DEBUG com.dz.demo.multiThread.LockSupportProducer - cooking over盘寡,time is 15
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is rice,time is 10
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is beef,time is 11
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is chicken,time is 12
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is beef,time is 13
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating ,delicious food is potato,time is 14
20:30:03.773 [pool-1-thread-2] DEBUG com.dz.demo.multiThread.LockSupportConsumer - eating over楚殿,time is 15
  • 在分布式環(huán)境下的生產(chǎn)者消費(fèi)者模型問題需要借助消息隊(duì)列mq或者redis的list類型實(shí)現(xiàn)。這個(gè)后續(xù)文章講到mq與redis會分享相關(guān)實(shí)踐

本篇講到了常見的三種多線程協(xié)作的模型宴抚,主要講述了解決生產(chǎn)者消費(fèi)者模型的三種常用辦法勒魔,多線程協(xié)作的其他兩個(gè)模型和相關(guān)api的使用將在下一篇中進(jìn)行講解

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市菇曲,隨后出現(xiàn)的幾起案子冠绢,更是在濱河造成了極大的恐慌,老刑警劉巖常潮,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弟胀,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)孵户,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門萧朝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人夏哭,你說我怎么就攤上這事检柬。” “怎么了竖配?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵何址,是天一觀的道長。 經(jīng)常有香客問我进胯,道長用爪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任胁镐,我火速辦了婚禮偎血,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘盯漂。我一直安慰自己颇玷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布宠能。 她就那樣靜靜地躺著亚隙,像睡著了一般磁餐。 火紅的嫁衣襯著肌膚如雪违崇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天诊霹,我揣著相機(jī)與錄音羞延,去河邊找鬼。 笑死脾还,一個(gè)胖子當(dāng)著我的面吹牛伴箩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鄙漏,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼嗤谚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了怔蚌?” 一聲冷哼從身側(cè)響起巩步,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎桦踊,沒想到半個(gè)月后椅野,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年竟闪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了离福。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,912評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡炼蛤,死狀恐怖妖爷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情理朋,我是刑警寧澤赠涮,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站暗挑,受9級特大地震影響笋除,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜炸裆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一垃它、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧烹看,春花似錦国拇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至土思,卻和暖如春务热,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背己儒。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工崎岂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人闪湾。 一個(gè)月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓冲甘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親途样。 傳聞我的和親對象是個(gè)殘疾皇子伟件,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容