zoukankan      html  css  js  c++  java
  • LinkedBlockingQueue 实现 生产者 消费者

    转载:https://blog.csdn.net/sinat_36553913/article/details/79533606

    Java中使用LinkedBlockingQueue实现生产者,消费者模式

    LinkedBlockingQueue实现是线程安全的,实现了FIFO(先进先出)等特性. 是作为生产者消费者的首选,LinkedBlockingQueue 可以指定容量,也可以不指定,不指定的话,默认最大是Integer.MAX_VALUE,其中主要用到put和take方法,put方法在队列满的时候会阻塞直到有队列成员被消费,take方法在队列空的时候会阻塞,直到有队列成员被放进来。

    生产者实现

    [java] view plain copy
     
    1. package cn.hpc.producerConsumer;  
    2.   
    3. import java.util.UUID;  
    4. import java.util.concurrent.BlockingQueue;  
    5.   
    6. public class Producer implements Runnable {  
    7.     private BlockingQueue<String> queue;  
    8.     private String produce;  
    9.     public Producer(BlockingQueue<String> queue, String produce) {  
    10.         this.queue = queue;  
    11.         if (null != produce)  
    12.             this.produce = produce;  
    13.         else this.produce = "null ";  
    14.     }  
    15.   
    16.     @Override  
    17.     public void run() {  
    18.         String uuid = UUID.randomUUID().toString();  
    19.         try {  
    20.             Thread.sleep(200);//生产需要时间  
    21.             queue.put(produce + " : " + uuid);  
    22.             System.out.println("Produce "" + produce + "" : " + uuid + " " + Thread.currentThread());  
    23.               
    24.         } catch (InterruptedException e) {  
    25.             System.out.println(e.getMessage());  
    26.         }  
    27.     }  
    28. }         
     

    消费者


    [java] view plain copy

     
     
     
    1. package cn.hpc.producerConsumer;  
    2. import java.util.concurrent.BlockingQueue;    
    3. public class Consumer implements Runnable {  
    4.     private BlockingQueue<String> queue;  
    5.     private String consumer;    
    6.     public Consumer(BlockingQueue<String> queue, String consumer) {  
    7.         this.queue = queue;  
    8.         if (null != consumer)  
    9.             this.consumer = consumer;  
    10.         else  
    11.             this.consumer = "null ";  
    12.     }  
    13.   
    14.     @Override  
    15.     public void run() {  
    16.         try {  
    17.             String uuid = queue.take();  
    18.             System.out.println(consumer + " decayed " + uuid  
    19.                     + " " + Thread.currentThread());  
    20.         } catch (InterruptedException e) {  
    21.             System.out.println(e.getMessage());  
    22.         }  
    23.     }  
    24. }  

    调用 :  new Tester();

    [java] view plain copy
     
    1. package cn.hpc.producerConsumer;  
    2. import java.util.concurrent.ExecutorService;  
    3. import java.util.concurrent.Executors;  
    4. import java.util.concurrent.LinkedBlockingQueue;  
    5.   
    6. public class Tester {  
    7.   
    8.     public Tester(){  
    9.         // 队列  
    10.         LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(10); 
    11.         ExecutorService service = Executors.newCachedThreadPool();  
    12.         for (int i = 0; i < 6; i++) {  
    13.             service.submit(new Consumer(queue, "X二代" + i));  
    14.             service.submit(new Consumer(queue, "导演" + i));  
    15.         }  
    16.         for (int i = 0; i < 6; i++) {  
    17.             service.submit(new Producer(queue, "黄金酒," + i));  
    18.             service.submit(new Producer(queue, "美女演员" + i));  
    19.         }  
    20.         service.shutdown();  
    21.     }  
    22. }      

    看看输出日志 n copy

     
     
     
    1. 12-26 12:13:07.689: I/System.out(19372): Produce "黄金酒0" : 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[pool-1-thread-13,5,main]  
    2. 12-26 12:13:07.699: I/System.out(19372): 导演4 decayed 美女演员4 : 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-10,5,main]  
    3. 12-26 12:13:07.699: I/System.out(19372): Produce "黄金酒1" : 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-15,5,main]  
    4. 12-26 12:13:07.699: I/System.out(19372): X二代5 decayed 黄金酒5 : 66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-11,5,main]  
    5. 12-26 12:13:07.699: I/System.out(19372): Produce "黄金酒5" : 66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-23,5,main]  
    6. 12-26 12:13:07.699: I/System.out(19372): 导演5 decayed 美女演员5 : d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-12,5,main]  
    7. 12-26 12:13:07.699: I/System.out(19372): Produce "美女演员1" : 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-16,5,main]  
    8. 12-26 12:13:07.699: I/System.out(19372): Produce "美女演员3" : 72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-20,5,main]  
    9. 12-26 12:13:07.699: I/System.out(19372): X二代2 decayed 黄金酒2 : 75f1952c-975d-41b1-ba69-9e24006031cd Thread[pool-1-thread-5,5,main]  
    10. 12-26 12:13:07.699: I/System.out(19372): 导演2 decayed 美女演员2 : adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-6,5,main]  
    11. 12-26 12:13:07.699: I/System.out(19372): 导演3 decayed 美女演员3 : 72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-8,5,main]  
    12. 12-26 12:13:07.699: I/System.out(19372): Produce "黄金酒2" : 75f1952c-975d-41b1-ba69-9e24006031cd Thread[pool-1-thread-17,5,main]  
    13. 12-26 12:13:07.699: I/System.out(19372): Produce "美女演员0" : aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-14,5,main]  
    14. 12-26 12:13:07.699: I/System.out(19372): X二代0 decayed 黄金酒0 : 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[pool-1-thread-1,5,main]  
    15. 12-26 12:13:07.699: I/System.out(19372): 导演0 decayed 美女演员0 : aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-2,5,main]  
    16. 12-26 12:13:07.699: I/System.out(19372): X二代1 decayed 黄金酒1 : 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-3,5,main]  
    17. 12-26 12:13:07.699: I/System.out(19372): 导演1 decayed 美女演员1 : 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-4,5,main]  
    18. 12-26 12:13:07.699: I/System.out(19372): Produce "黄金酒3" : c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-19,5,main]  
    19. 12-26 12:13:07.699: I/System.out(19372): Produce "美女演员5" : d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-24,5,main]  
    20. 12-26 12:13:07.699: I/System.out(19372): Produce "黄金酒4" : 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-21,5,main]  
    21. 12-26 12:13:07.699: I/System.out(19372): Produce "美女演员2" : adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-18,5,main]  
    22. 12-26 12:13:07.699: I/System.out(19372): Produce "美女演员4" : 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-22,5,main]  
    23. 12-26 12:13:07.699: I/System.out(19372): X二代3 decayed 黄金酒3 : c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-7,5,main]  
    24. 12-26 12:13:07.699: I/System.out(19372): X二代4 decayed 黄金酒4 : 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-9,5,main]  
  • 相关阅读:
    CSS之各种居中
    三步教会你装系统
    65条最常用正则表达式
    MongoDB介绍
    MongoDB基本命令用
    log4j配置
    使用spring + ActiveMQ 总结
    log4j配置文件
    如何入侵局域网电脑
    目标检测的图像特征提取
  • 原文地址:https://www.cnblogs.com/renjiaqi/p/10248067.html
Copyright © 2011-2022 走看看