生产者和消费者案例:
package BoxDemo;
public class Box {
private int milk;
//定义一个成员变量,表示奶箱的状态
private boolean state = false;
public synchronized void put(int milk){
//如果有牛奶,等待消费
if (state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有牛奶,就生产牛奶
this.milk = milk;
System.out.println("送奶工将第:"+this.milk+"放入奶箱");
//生产完毕之后,修改牛奶箱的状态
state = true;
//唤醒其他等待的线程
notifyAll();
}
public synchronized void getMilk(){
//如果没有牛奶,等待生产
if(!state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果有牛奶就消费牛奶
System.out.println("用户拿到第"+this.milk+"瓶奶");
//消费完毕之后,修改奶箱状态
state = false;
//唤醒其他等待的线程
notifyAll();
}
}
package BoxDemo;
public class BoxTest {
public static void main(String[] args) {
Box b = new Box();
Producer p = new Producer(b);
Customer c = new Customer(b);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
package BoxDemo;
public class Producer implements Runnable{
private Box b;
public Producer(Box b) {
this.b = b;
}
@Override
public void run() {
for (int i = 1; i < 10; i++) {
b.put(i);
}
}
}
package BoxDemo;
public class Customer implements Runnable{
private Box b;
public Customer(Box b) {
this.b = b;
}
@Override
public void run() {
while (true){
b.getMilk();
}
}
}