多线程的设计模式:生产者消费者
两类:
1.单生产者单消费者
2.多生产者多消费者
先研究单生产者单消费者
分析:
生产线程,消费线程
生产任务,消费任务
产品
``
`public class Demo4 {
public static void main(String[] args) {
//1.创建产品
Product product = new Product();
//2.创建生产任务,消费任务
Producer producer = new Producer(product);//生产
Consumer consumer = new Consumer(product);//消费
//3.创建生产线程,消费线程
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
//4.开启线程
t1.start();
t2.start();
}
}
//创建产品类
class Product{
String name;
double price;
int count;
boolean flag = false;//用来在唤醒与等待之间进行切换
//为生产准备数据
public synchronized void setProduce(String name,double price){
if (flag == true) {
try {
wait();//让生产线程等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name;
this.price = price;
System.out.println(Thread.currentThread().getName()+" 生产了:"+name+" 产品的数量:"+count+" 产品的价格:"+price);
count++;
flag = ! flag;
notify();//唤醒消费线程
}
//为消费准备数据
public synchronized void consume() {
if (flag == false) {
try {
wait();//让消费线程等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" 消费了:"+name+" 产品的数量:"+count+" 产品的价格:"+price);
flag = ! flag;
notify();//唤醒生产线程
}
}
//创建生产任务
class Producer implements Runnable{
Product product;
public Producer(Product product) {
super();
this.product = product;
}
public void run() {
while (true) {
product.setProduce("bingbing", 10);
}
}
}
//创建消费任务
class Consumer implements Runnable{
Product product;
public Consumer(Product product) {
super();
this.product = product;
}
public void run() {
while (true) {
product.consume();
}
}
}
---
```多线程的设计模式:生产者消费者
两类:
1.单生产者单消费者
2.多生产者多消费者
先研究多生产者多消费者
分析:
生产线程,消费线程有多个
生产任务,消费任务各有一个
产品一个
public class Demo5 {
public static void main(String[] args) {
//1.创建产品
Product1 product = new Product1();
//2.创建生产任务,消费任务
Producer1 producer = new Producer1(product);
Consumer1 consumer = new Consumer1(product);
//3.创建生产线程,消费线程
Thread t0 = new Thread(producer);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
Thread t3 = new Thread(consumer);
//4.开启线程
t0.start();
t1.start();
t2.start();
t3.start();
}
}
//创建产品类
class Product1{
String name;
double price;
int count;
boolean flag = false;//用来在唤醒与等待之间进行切换
//为生产准备数据
public synchronized void setProduce(String name,double price){
while (flag == true) {
try {
wait();//让生产线程等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name;
this.price = price;
System.out.println(Thread.currentThread().getName()+" 生产了:"+name+" 产品的数量:"+count+" 产品的价格:"+price);
count++;
flag = ! flag;
//notify();//唤醒消费线程
notifyAll();
}
//为消费准备数据
public synchronized void consume() {
while (flag == false) {
try {
wait();//让消费线程等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" 消费了:"+name+" 产品的数量:"+count+" 产品的价格:"+price);
flag = ! flag;
//notify();//唤醒生产线程
notifyAll();
}
}
//创建生产任务
class Producer1 implements Runnable{
Product1 product;
public Producer1(Product1 product) {
super();
this.product = product;
}
public void run() {
while (true) {
product.setProduce("bingbing", 10);
}
}
}
//创建消费任务
class Consumer1 implements Runnable{
Product1 product;
public Consumer1(Product1 product) {
super();
this.product = product;
}
public void run() {
while (true) {
product.consume();
}
}
}