Producers and consumers, seeking answers to the great God

Today, he wrote a producer-consumer model, there is no problem in a single producer and consumer of exercise, a production of a data consumption data;

But when a consumer adds time, producers will produce a negative product came out, my judgment is, when the product is <0; consumers are blocked, but

Did not seem to succeed, still produce negative product;

Pursuing big God answering questions;

The following code is posted

package Thread_p_c;

public class Producer implements Runnable {
	private Product p;
	public Producer (Product p) {
		this.p = p;
	}
	@Override
	public void run() {
		while(true) {
			synchronized (p) {
				if(!p.isFlag()) {
					try {
						p.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					System.out.println("生产者开始制造产品:");
					int i = p.getPno();
					i++;
					p.setPno(i);
					p.setFlag(false);
					System.out.println ( "producers to produce the" + p.getPname () + "->" + p.getPno ());
					System.out.println ( "producers end of production, consumption wait!"); 
				
					P.notify (); 
					} 
				
				
			
		} 
	} 

}

  

Thread_p_c Package; 

public class Consumer the implements the Runnable { 
	Private Product P; 
	public Consumer (Product P) { 
		this.p = P; 
	} 
	@Override 
	public void RUN () { 
		the while (to true) { 
			the synchronized (P) { 
				IF (p.isFlag () || p.getPno () <= 0) { 
					the try { 
						p.wait (); 
					} the catch (InterruptedException E) { 
						// the TODO Auto-Generated Block the catch 
						e.printStackTrace (); 
					} 
					} 
					System.out.println ( "consumers into consumer shopping"); 
					System.out.println ( "consumers are consumer products:"); 
					System.out.println ( "consumer spending in ......."); 
					int i = p.getPno ();
					System.out.println ( "end consumer spending, consumption of" + p.getPname () + ":" + p.getPno ()); 
					i--; 
					p.setPno (i); 
					p.setFlag (to true) ; 
					p.notify (); 
					System.out.println ( "consumer end, is to inform manufacturers to produce ..."); 
				
					
			} 
				
				
		} 
	} 

}

  

package Thread_p_c;

public class Product {
	private int pno=0;
	public Product(String pname) {
		this.pname=pname;
	}
	private String pname ="灯泡";
	private boolean flag = true;
	
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public int getPno() {
		return pno;
	}
	public void setPno(int pno) {
		this.pno = pno;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
}

  

package Thread_p_c;

public class pc_model {
	private int i = 0;
	private boolean flag = true;

	public static void main(String[] args) {
		Product p = new Product("灯泡");
		Thread t1 = new Thread(new Producer(p));
		Thread t2 = new Thread(new Consumer(p));
		t1.start();
		t2.start();
//		new Thread(new Producer(p)).start();
//		new Thread(new Consumer(p)).start();
	}

}

  

 

Guess you like

Origin www.cnblogs.com/hack9527/p/11706506.html