package com.thread_;
//管程法解决生产者消费者模型
public class PC1 {
public static void main(String[] args) {
SynContainer container = new SynContainer();
Productor productor = new Productor(container);
Consumer consumer = new Consumer(container);
productor.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
consumer.start();
}
}
//生产者
class Productor extends Thread{
SynContainer container ;
public Productor (SynContainer container){
this.container = container;
}
//生产
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("生产了第" + i + "个产品");
container.pP(new Product(i));
}
}
}
//消费者
class Consumer extends Thread{
SynContainer container ;
public Consumer (SynContainer container){
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了第" + container.cC().id + "件产品");
}
}
}
//产品
class Product {
int id ;
public Product(int id) {
this.id = id;
}
}
//缓存区
class SynContainer{
//容器
Product[] products = new Product[10];
//计数器
int count = 0;
//生产者放入产品
public synchronized void pP(Product product){
//如果容器满,需要等待消费
if (count == products.length){
//通知消费者消费.生产等待
System.out.println("等待消费!");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没满,放入产品
products[count] = product;
count++;
//通知消费者消费
this.notifyAll();
}
//消费者消费产品
public synchronized Product cC(){
//判断能否消费
if (count == 0){
//等待生产
System.out.println("等待生产!");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//消费
count--;
Product product = products[count];
//消费结束,通知生产者生产
this.notifyAll();
return product;
}
}
package com.thread_;
//信号灯法解决生产者消费者模型
public class PC2 {
public static void main(String[] args) throws Exception{
Product01 product01 = new Product01();
new Productor01(product01).start();
new Consumer01(product01).start();
}
}
//生产者
class Productor01 extends Thread{
Product01 product01 ;
public Productor01(Product01 product01){
this.product01 = product01;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2 == 0){
this.product01.pP01("生产了一盒饼干");
}else {
this.product01.pP01("生产了一盒牛奶");
}
}
}
}
//消费者
class Consumer01 extends Thread{
Product01 product01 ;
public Consumer01(Product01 product01){
this.product01 = product01;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
product01.cC01();
}
}
}
//产品
class Product01 {
//生产者生产,消费者等待 T
//消费者消费,生产者等待 F
String production; //产品
boolean flag = true;
//生产
public synchronized void pP01(String production){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产者生产了" + production);
//通知消费者消费
this.notifyAll();
this.production = production;
this.flag = !flag;
}
//消费
public synchronized void cC01(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者消费" + production);
//通知生产者生产
this.notifyAll();
this.flag = !flag;
}
}
标签:container,product01,管程,练习,class,信号灯,void,new,public
From: https://www.cnblogs.com/Q1u-ovo/p/17364420.html