当前位置: 首页 > news >正文

用java实现生产者和消费者问题

package javaTest;


//测试类   
 public class ProCon{ 
   
	 public static void main(String[] args){  
		 SyncQueue queue = new SyncQueue();  
		 Consumer p = new Consumer(queue);  
		 Producer c = new Producer(queue);  
		   
		   
		 new Thread(p).start();  
		 new Thread(c).start();  
	 }  
 }  
 //生产者  
 class Producer implements Runnable{   
     private SyncQueue queue;  
     private int count = 0;
     public Producer(SyncQueue queue){  
    	 this.queue = queue;  
     }  
     public void run(){  
	     while(true){  
		     String product = "产品"+count;  
		     queue.put(product);  
		     count++;
		     System.out.println("生产了: "+product);  
		     try{  
		    	 Thread.sleep(2000);  
		     }catch(InterruptedException e){  
		    	 e.printStackTrace();  
		     }  
	    }  
     }  
 }  
//消费者    
 class Consumer implements Runnable{   
    private SyncQueue queue;  
   
    public Consumer(SyncQueue queue) {  
    this.queue = queue;  
     }  
      
    public void run(){  
	    while(true){  
	    	String product = queue.get();  
	    	System.out.println("消费了: "+product);  
	    	try{  
	    		Thread.sleep(2000);  
	    	}catch(InterruptedException e){  
	    		e.printStackTrace();  
	    	}  
	    }  
    }  
 }  
 //公共缓冲区 
 class SyncQueue{   
	private String[] queue = new String[10];  
    private int index = 0;  
    //生产者把产品放入队列
    public synchronized void put(String productor){ 
	    if(index == queue.length){  
	      try{  
	       wait();  
	      }catch(InterruptedException e){  
	        e.printStackTrace();  
	       }  
	    }		    
		queue[index] = productor;  
		index++;  
		//唤醒在此对象监视器上等待的消费者线程  
		notify(); 
    }  
    //消费者从队列中取出一个产品消费
    public synchronized String get(){     
	    if(index == 0){  
	    	try{  
	    		wait();  
		    }catch (InterruptedException e){  
		        e.printStackTrace();  
		    }  
	    }
		index--;  
		String product = queue[index]; 
		//唤醒在此对象监视器上等待的生产者线程
		notify();  
		return product;  
    }  

 }  


相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【转】AngularJS 日期格式化 字典
  • Struts的线程安全问题
  • JSP中的pageEncoding和contentType的区别
  • 2016-wing的年度总结
  • java中split() replace() replaceAll()三个函数分析
  • SPOJ-COLONY - Linearian Colony!简单二分思想
  • msfconsole 控制台使用和操作
  • 数据库范式
  • sed awk grep三剑客常用
  • 数据库事务隔离级别
  • 全排序算法
  • 用Spring+Junit4.4进行测试(使用注解)
  • Java HashMap 分析四篇连载
  • Leetcode 144. Binary Tree Preorder Traversal
  • 单页web应用是什么?它又会给传统网站带来哪些好处?
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • JAVA 学习IO流
  • JS字符串转数字方法总结
  • Python利用正则抓取网页内容保存到本地
  • Python连接Oracle
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 大数据与云计算学习:数据分析(二)
  • 第十八天-企业应用架构模式-基本模式
  • 反思总结然后整装待发
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 每天10道Java面试题,跟我走,offer有!
  • 如何使用 JavaScript 解析 URL
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 一天一个设计模式之JS实现——适配器模式
  • 移动端唤起键盘时取消position:fixed定位
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 7行Python代码的人脸识别
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • mysql面试题分组并合并列
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​如何防止网络攻击?
  • #if和#ifdef区别
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • ( 10 )MySQL中的外键
  • (2)leetcode 234.回文链表 141.环形链表
  • (3)nginx 配置(nginx.conf)
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (k8s)Kubernetes本地存储接入
  • (LLM) 很笨
  • (第30天)二叉树阶段总结
  • (二)学习JVM —— 垃圾回收机制
  • (二十六)Java 数据结构
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (一)、软硬件全开源智能手表,与手机互联,标配多表盘,功能丰富(ZSWatch-Zephyr)
  • (转)大型网站架构演变和知识体系
  • **《Linux/Unix系统编程手册》读书笔记24章**
  • .“空心村”成因分析及解决对策122344
  • .ai域名是什么后缀?
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)
  • .net6Api后台+uniapp导出Excel