生产者消费者问题

复制代码
import java.util.*;
public class ProducerConsumerTest{
    final int BUFFER_SIZE=10;//缓冲区最大值
    Queue<Integer> queue;//共享缓冲队列
    public ProducerConsumerTest(){
        queue=new LinkedList<Integer>();
    }
    public static void main(String[] args){
        ProducerConsumerTest PC=new ProducerConsumerTest();
        int count=100;
        System.out.println("开始生产消费了哦");
        while(count-->0){
            new Thread(PC.new Producer()).start();
            new Thread(PC.new Consumer()).start();
        }
        
    }
    class Consumer implements Runnable{
        @Override
        public void run() {
            synchronized(queue){
                if(queue.size()==0){
                    try {
                        queue.wait();//释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    int x=queue.remove();
                    System.out.println("消费 :"+x);
                    queue.notify();//唤醒生产者
                }
                
            }
        }
    }
    class Producer implements Runnable{

        @Override
        public void run() {
            synchronized(queue){
                if(queue.size()==BUFFER_SIZE){
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }    
                }else{
                    int x=queue.size()+1;
                    queue.add(x);
                    System.out.println("生产 :"+x);
                    queue.notify();
                }
            }
        }
        
    }
    
}
复制代码

 加上Executor,BlockingQueue

复制代码
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;


public class ProducerConsumerTest{
    final int BUFFER_SIZE=10;
    final String[] produce=new String[5];
    LinkedBlockingQueue<String> queue;
    public ProducerConsumerTest(){
        queue=new LinkedBlockingQueue<String>(BUFFER_SIZE);
        produce[0]="水";
        produce[1]="面包";
        produce[2]="空气";        
        produce[3]="快乐";
        produce[4]="灾难";
    }
    public static void main(String[] args) throws InterruptedException{
        ProducerConsumerTest PC=new ProducerConsumerTest();
        System.out.println("开始生产消费了哦");
        ExecutorService exec=Executors.newCachedThreadPool();
        exec.execute(new Thread(PC.new Producer()));
        TimeUnit.MILLISECONDS.sleep(1000);//先给我生产一会,好吧
        exec.execute(new Thread(PC.new Consumer()));
        exec.shutdown();
    }
    class Consumer implements Runnable{
        @Override
        public void run() {    
            while(true){
                try {
                    String p = queue.take();
                    System.out.println("消费 :"+p);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
            }    
        }
    }
    class Producer implements Runnable{        
        @Override
        public void run() {
            Random rand=new Random();
            int count=10000;
            while(count-->0){
                try {
                    int x=rand.nextInt(5);
                    queue.put(produce[x]);
                    System.out.println("生产 :"+produce[x]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
}
复制代码

 

posted @   wqkant  阅读(159)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示