承接上篇博文,http://blog.csdn.net/u012954720/article/details/51757964
,谈到生产和消费并不是一对一的,即生产完立即消费,他们的协作出现了问题。这篇博文将解决这个问题。
package com.xiaofeng.example;
/**
* 生产者、消费者案例
*
* @author XiaoFeng1015
*/
public class TheadDemo5 {
public static void main(String[] args) {
Food food = new Food();
Producter p = new Producter(food);
Customers c = new Customers(food);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
class Producter implements Runnable {
private Food food;
public Producter(Food food) {
this.food = food;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
if (i % 2 == 0) {
// food.setName("银耳莲子汤!");
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// food.setEfficasy("功效:美容养颜!");
food.set("银耳莲子汤!", "功效:美容养颜!");
} else {
// food.setName("糖醋里脊!");
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// food.setEfficasy("功效: 酸甜可口!");
food.set("糖醋里脊!", "功效: 酸甜可口!");
}
}
}
}
class Customers implements Runnable {
private Food food;
public Customers(Food food) {
this.food = food;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println(food.getEfficasy() + "-->" + food.getName());
food.get();
}
}
}
// 使用同步方法将生产和消费包裹起来。
class Food {
private String name;
private String efficasy;
// true表示可以生产
private boolean flag = true;
public Food() {
super();
}
public Food(String name, String efficasy) {
super();
this.name = name;
this.efficasy = efficasy;
}
public synchronized void set(String name, String efficasy) {
// 表示不能生产
if (!flag) {
try {
// 当前的线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或
// notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。
// 当前线程进入等待状态,让出CPU,并释放该监视器上的锁。
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setEfficasy(efficasy);
flag = false;
// 生产完毕之后,记得唤醒该监视器上的其他一个线程
this.notify();
}
public synchronized void get() {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 加休眠的目的:为了防止get在一开始时取空
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + "-->" + this.getEfficasy());
// 表示不能在取
flag = true;
this.notify();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEfficasy() {
return efficasy;
}
public void setEfficasy(String efficasy) {
this.efficasy = efficasy;
}
}
运行结果:
sleep(); wait();的区别:
sleep():休眠一段时间,让出CPU,不释放当前监视器
wait():等待一段时间,让出CPU,释放当前监视器。