package com.vince;
/**
* 兩個線程協同工作,先生產,再消費
* wait():當前線程停止運行,被同一個線程鎖的其它線程喚醒之後才能接着運行
* notify(): 喚醒同一個同步鎖中某一個wait()鎖
*
* sleep和wait的區別:
* sleep: 讓線程進入休眠狀態,讓出CPU的時間片,不釋放對象監視器的所有權(對象鎖)
* wait:讓線程進入等待狀態,讓出CPU的時間片,並釋放對象監視器的所有權(對象鎖),等待其它線程通過notify方法喚醒
*/
public class ProducterCustomerDemo {
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 Customers implements Runnable{
private Food food;
public Customers(Food food){
this.food = food;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
food.get();
}
}
}
/**
* 生產者
*/
class Producter implements Runnable{
private Food food;
public Producter(Food food){
this.food = food;
}
@Override
public void run() {
for(int i = 0; i < 5; i++){
if(i %2 == 0){
food.set("鍋巴肉","酸甜口味,爽");
}else{
food.set("佛跳牆","大補,滋陰補陽");
}
}
}
}
/**
* 食物
*/
class Food{
private String name;
private String desc;//描述
private boolean flag = true; //true表示可以生產,false表示可以消費
/**
* 生產產品
*/
public synchronized void set(String name,String desc){
//不能生產
if(!flag){
try {
this.wait();//線程進入等待狀態,釋放監視器的所有權(對象鎖)
} 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.notify();//喚醒等待的線程(隨機其中一個)
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Food(String name, String desc) {
this.name = name;
this.desc = desc;
}
public Food() {
}
@Override
public String toString() {
return "Food{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}