* /**
* 生产者和消费者
* @author wuyinlei
*
*/
public class ThreadDemo {
public static void main(String[]args){
Food food = new Food();
Producter p = new Producter(food);
Customer c = new Customer(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() {
//生产100份菜
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
food.setName("东北大乱炖");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
food.setEfficasy("好吃不贵真实惠");
} else{
food.setName("糖醋鲤鱼");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
food.setEfficasy("酸甜口");
}
}
}
}
//消费者
class Customer implements Runnable{
private Food food;
public Customer (Food food){
this.food = food;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(food.getName() + "--->" + food.getEfficasy());
}
}
}
//消费的对象
class Food{
public Food() {
super();
}
public Food(String name, String efficasy) {
super();
this.name = name;
this.efficasy = efficasy;
}
private String name; //菜名
private String efficasy; //功效
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;
}
}
下面我们来下同步(对food的操作,那么我们就来对food进行同步)
package com.yinlei.thread;
import java.awt.PageAttributes;
/**
* 生产者和消费者
*
* @author wuyinlei
*
*/
public class ThreadDemo {
public static void main(String[] args) {
Food food = new Food();
Producter p = new Producter(food);
Customer c = new Customer(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() {
// 生产100份菜
for (int i = 0; i < 100; 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 Customer implements Runnable {
private Food food;
public Customer(Food food) {
this.food = food;
}
@Override
public void run() {
/*
* for (int i = 0; i < 100; i++) { try { Thread.sleep(500); } catch
* (InterruptedException e) { e.printStackTrace(); }
* System.out.println(food.getName() + "--->" + food.getEfficasy());
*/
food.get();
}
}
// 消费的对象
class Food {
private boolean flag = true;// true表示可以生产 false表示可以消费
public Food() {
super();
}
public Food(String name, String efficasy) {
super();
this.name = name;
this.efficasy = efficasy;
}
private String name; // 菜名
private String efficasy; // 功效
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;
}
// 生产产品
public synchronized void set(String name, String efficasy) {
// 表示不能生产
if (!flag) {
try {
this.wait(); // 当前线程进入等待状态,但是会让出cpu,并且释放锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setEfficasy(efficasy);
flag = false; // 表示不能生产
this.notify(); // 唤醒该监视器上的其他线程
}
// 消费产品
public synchronized void get() {
if (flag) {
try {
this.wait(); // 当前线程进入等待状态,但是会让出cpu,并且释放锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + "--->" + this.getEfficasy());
flag = true; // 表示不能再取
this.notify(); // 唤醒该监视器上的其他线程
}
}