生产者消费者模式

image.png

image.png

image.png

image.png
管程法
/**
* 线程协作--生产者消费者模式(模拟麦当劳点鸡)--管程法
*/
public class PCDemo {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Thread(new Productor(container)).start();
new Thread(new Consumer(container)).start();
}
}
//生产者
class Productor implements Runnable{
SynContainer container;
public Productor(SynContainer container) {
this.container = container;
}
@Override
public void run() {
//生产100只鸡
for (int i = 1; i <= 100; i++) {
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
container.push(new Chicken(i));
}
}
}
//消费者
class Consumer implements Runnable{
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
//消费100只鸡
for (int i = 1; i <= 100; i++) {
try {
TimeUnit.MICROSECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
container.pop();
}
}
}
//产品 -- 鸡
class Chicken{
int id;
public Chicken(int id) {
this.id = id;
}
}
//容器
class SynContainer {
Chicken[] chickens = new Chicken[10];
int count;//柜台有多少只鸡
//生产者生产鸡
public synchronized void push(Chicken chicken){
//如果满了,等待消费者消费
if(count == chickens.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//没满,把鸡放到柜台
chickens[count++] = chicken;
System.out.println("生产了第 " + chicken.id +" 只鸡,柜台共有" + count + "只鸡");
//通知消费者消费
this.notify();
}
//消费者消费鸡
public synchronized Chicken pop(){
Chicken chicken = null;
//如果柜台没有,等待生产者生产
if(count == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果有,消费
chicken = chickens[--count];
System.out.println("消费了第 " + chicken.id +" 只鸡,柜台共有" + count + "只鸡");
//通知生产者生产
this.notifyAll();
return chicken;
}
}
信号灯法
**
* 线程协作--生产者消费者模式(模拟观众观看演员节目)--信号灯法
*/
public class SignalLightDemo {
public static void main(String[] args) {
Tv tv = new Tv();
new Thread(new Player(tv)).start();
new Thread(new Watcher(tv)).start();
}
}
//生产者 -- 演员
class Player implements Runnable{
private Tv tv;
public Player(Tv tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.play("快乐大本营--" + i);
}
}
}
//消费者 -- 观众
class Watcher implements Runnable{
private Tv tv;
public Watcher(Tv tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//产品 -- 节目
class Tv {
private String voice;//节目内容
// 标志位:演员表演节目,观众等待 T
// 观众观看节目,演员等待 F
private boolean flag = true;
//演员 -- 表演节目
public synchronized void play(String voice){
// 观众在观看节目,演员等待
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//演员表演了节目,并通知观众观看
this.voice = voice;
this.flag = !this.flag;
System.out.println("演员表演了:" + this.voice);
this.notifyAll();
}
//观众 -- 观看节目
public synchronized void watch(){
// 演员在表演节目,观众等待
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 观众观看了节目,并通知演员表演
this.flag = !this.flag;
System.out.println("观众观看了:"+this.voice);
this.notifyAll();
}
}
线程池

image.png

image.png
/**
* 线程池:service.execute(Runnable);
*/
public class ExcutorDemo {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyRunnable());
service.execute(new MyRunnable());
service.execute(new MyRunnable());
service.execute(new MyRunnable());
service.shutdown();
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}