生產(chǎn)者消費者模式

image.png

image.png

image.png

image.png
管程法
/**
* 線程協(xié)作--生產(chǎn)者消費者模式(模擬麥當勞點雞)--管程法
*/
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();
}
}
//生產(chǎn)者
class Productor implements Runnable{
SynContainer container;
public Productor(SynContainer container) {
this.container = container;
}
@Override
public void run() {
//生產(chǎn)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();
}
}
}
//產(chǎn)品 -- 雞
class Chicken{
int id;
public Chicken(int id) {
this.id = id;
}
}
//容器
class SynContainer {
Chicken[] chickens = new Chicken[10];
int count;//柜臺有多少只雞
//生產(chǎn)者生產(chǎn)雞
public synchronized void push(Chicken chicken){
//如果滿了,等待消費者消費
if(count == chickens.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//沒滿,把雞放到柜臺
chickens[count++] = chicken;
System.out.println("生產(chǎn)了第 " + chicken.id +" 只雞,柜臺共有" + count + "只雞");
//通知消費者消費
this.notify();
}
//消費者消費雞
public synchronized Chicken pop(){
Chicken chicken = null;
//如果柜臺沒有,等待生產(chǎn)者生產(chǎn)
if(count == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果有,消費
chicken = chickens[--count];
System.out.println("消費了第 " + chicken.id +" 只雞,柜臺共有" + count + "只雞");
//通知生產(chǎn)者生產(chǎn)
this.notifyAll();
return chicken;
}
}
信號燈法
**
* 線程協(xié)作--生產(chǎn)者消費者模式(模擬觀眾觀看演員節(jié)目)--信號燈法
*/
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();
}
}
//生產(chǎn)者 -- 演員
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();
}
}
}
//產(chǎn)品 -- 節(jié)目
class Tv {
private String voice;//節(jié)目內(nèi)容
// 標志位:演員表演節(jié)目,觀眾等待 T
// 觀眾觀看節(jié)目,演員等待 F
private boolean flag = true;
//演員 -- 表演節(jié)目
public synchronized void play(String voice){
// 觀眾在觀看節(jié)目,演員等待
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//演員表演了節(jié)目,并通知觀眾觀看
this.voice = voice;
this.flag = !this.flag;
System.out.println("演員表演了:" + this.voice);
this.notifyAll();
}
//觀眾 -- 觀看節(jié)目
public synchronized void watch(){
// 演員在表演節(jié)目,觀眾等待
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 觀眾觀看了節(jié)目,并通知演員表演
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());
}
}