JAVA线程同步的生产者和消费者问题
class productor extends Thread{
Integer product;
productor(Integer product){
this.product = product;
}
public void run(){
synchronized(product){
for(int i = 0;i < 5;i++){
product++;
System.out.println( "The product is: " + product);
product.notify();
try{
product.wait();
}catch(Exception e){
e.printStackTrace();
}
}
product.notify();
}
}
}
class Consumer extends Thread{
Integer consumer;
Consumer(Integer consumer){
this.consumer = consumer;
}
public void run(){
synchronized(consumer){
for(int i = 0;i < 5;i++){
System.out.println( "The consumer is: " + consumer);
consumer.notify();
try{
consumer.wait();
}catch(Exception e){
e.printStackTrace();