生产者与消费者
生产者不断生产产品,消费者不断取走产品
实例:饭店里有一个厨师和一个服务员,这个服务员必须等待厨师准备好膳食,当厨师准备好时,他会通知服务员上来,如何返回继续等待,这是一个任务协作的示例,厨师代表生产者,而服务员代表消费者
示例代码:
//两个线程协同工作,先生产再消费的过程
public class ThreadDemo2 {
public static void main(String[] args) {
Food f = new Food();
Producter p = new Producter(f);
Customers c = new Customers(f);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
//先调用set才能调用get
//生产者对象
class Producter implements Runnable{
private Food food;
//通过构造器把food传进来
public Producter(Food food){
this.food = food;
}
@Override
public void run() {
//模拟生产20份菜
for (int i = 0; i < 20; i++) {
if(i%2==0){
food.set("火锅","海底捞");
}else
{
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 < 20; i++) {
food.get();
}
}
}
//食物
class Food{
private String name;
private String desc;
private boolean flag = true;//true表示可以生产,flase表示可以消费
//生产产品
public synchronized void set(String name,String desc){
//不能生产时
if (!flag){
try {
this.wait(); //线程进入等待状态,释放监视器的所有权(对象锁),直到notify唤醒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setDesc(desc);
flag = false;
this.notify(); //唤醒等待的线程(随机的其中一个)
}
//消费产品
public synchronized void get(){
//不能消费
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+"->"+this.getDesc());
flag = true;
this.notifyAll();
}
public void setName(String name) {
this.name = name;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return super.toString();
}
public void Food(String name,String desc){
this.name = name;
this.desc = desc;
}
public void Food(){
}
}
运行效果:sleep与wait的区别:
sleep:让线程进入休眠状态,让出CPU的时间片,但是不释放对象监视器的所有权(对象锁)
wait:让线程进入等待状态,让出CPU的时间片,并释放对象监视器的所有权,等待其他线程通过notify方法来唤醒