1.多生产者多消费者问题
public class Demo10 {
public static void main ( String[ ] args) {
Product1 product = new Product1 ( ) ;
Producer1 producer = new Producer1 ( product) ;
Consumer1 consumer = new Consumer1 ( product) ;
Thread proThread1 = new Thread ( producer) ;
Thread proThread2 = new Thread ( producer) ;
Thread conThread1 = new Thread ( consumer) ;
Thread conThread2 = new Thread ( consumer) ;
proThread1. start ( ) ;
conThread1. start ( ) ;
proThread2. start ( ) ;
conThread2. start ( ) ;
}
}
class Product1 {
String name;
double price;
int count;
boolean flag = false ;
public synchronized void setProduce ( String name, double price) {
while ( flag == true ) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
this . name = name;
this . price = price;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " 生产了:" + this . name+ " 产品的数量:" + this . count+ " 价格:" + this . price) ;
count++ ;
flag =