用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;  
    }  

 }  


发布了66 篇原创文章 · 获赞 39 · 访问量 92万+
展开阅读全文

生产者消费者问题JAVA实现

08-10

``` public class TestProducerConsumer{ public static void main(String[] agrs){ SyncStack ss = new SyncStack(); Producer p = new Producer(ss); ConSumer c = new ConSumer(ss); new Thread(p).start(); new Thread(c).start(); } } class Woto{ int id; public Woto(int id){ this.id = id; } public String toString(){ return "Woto:" + id; } } class SyncStack{ int index = 0; Woto[] w = new Woto[8]; public synchronized void push(Woto wt){ while(index == w.length){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); w[index] = wt; index++; } public synchronized Woto pop(){ while(index == 0){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); index--; return w[index]; } } class Producer implements Runnable{ SyncStack ss = null; public Producer(SyncStack ss){ this.ss = ss; } public void run() { for(int i = 0;i<10;i++){ Woto wt = new Woto(i); ss.push(wt); System.out.println("生产了="+wt); try { Thread.sleep((int) (Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class ConSumer implements Runnable{ SyncStack ss = null; public ConSumer(SyncStack ss){ this.ss = ss; } public void run() { for(int i = 0;i<10;i++){ Woto wt =ss.pop(); System.out.println("消费了="+wt); try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 为什么会先出现消费者呢 ?求大神解答 ![图片说明](https://img-ask.csdn.net/upload/201508/10/1439197857_758696.png) 这里很明显是先start生产者 怎么可能先消费者呢 new Thread(p).start(); new Thread(c).start(); 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览