生产者消费者问题
线程通信的一个例子(一个应用场景)
线程通信的方法
线程之间的通信方法:
- wait() :让线程等待
- notify()、notifyAll() : 唤醒等待的线程
解决线程通信的方法1 :管程法
代码:
package Demo04;
//测试生产者消费者模式--》管程法:利用缓冲区解决。
//需要生产者productor、消费者consumer、产品、缓冲区四个对象
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consumer(container).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++) {
container.push(new Chicken(i));
System.out.println("生产了" + 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.pop().id + "只鸡");
}
}
}
//产品:鸡
class Chicken{
//产品需要一个编号
int id;
public Chicken(int id) {
this.id = id;
}
}
//缓冲区syncontainer:同步容器
class SynContainer{
//需要一个容器大小
Chicken[] chickens = new Chicken[10];
//容器计数器
int count = 0;
//生产者放入产品:synchronized关键字保证同步方法
public synchronized void push(Chicken chicken){
//如果容器满了,就需要通知消费者消费,生产等待;
if(count == chickens.length){
//生产者等待;
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果容器没有满,我们就需要丢入产品
chickens[count++] = chicken;
//就可以通知消费者消费了
this.notifyAll();
}
//消费者消费产品
public synchronized Chicken pop(){
//判断能否消费
if(count == 0){
//消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果可以消费
Chicken chicken = chickens[--count];
//吃完了,通知生产者生产
this.notifyAll();
return chicken;
}
}
解决线程通信的方法2 :信号灯法
- 代码例子:
假设:演员表演完,将节目上传后,观众才能观看;观众看完,演员才拍下一个节目,即:
演员表演,观众等待 T
观众观看,演员等待 F
package Demo04;
import java.sql.SQLOutput;
//测试生产者消费者模式2--》信号灯法:利用标志位解决。
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者--》演员
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv = tv;
}
@Override
public void run() {
//共播放20小时,一半节目,一般广告
for (int i = 0; i < 20; i++) {
if(i%2 == 0){
tv.play("吐槽大会");
}else{
tv.play("广告");
}
}
}
}
//消费者--》观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//产品---》节目
class TV{
//演员表演,观众等待 T
//观众观看,演员等待 F
String program; //表演的节目
boolean flag = true;
//表演
public synchronized void play(String program){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();//若flag = true 通知唤醒演员表演
System.out.println("演员表演了:" + program);
this.program = program;
this.flag = !this.flag;//表演完了,演员等待flag = false;
}
//观看
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}//通知演员表演
this.notifyAll();
System.out.println("观看了:" + program);
this.flag = !this.flag;//观众看完了,观众等待flag = true;
}
}
- 结果
线程池
package Demo04;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestPool {
public static void main(String[] args) {
//1.创建服务。创建线程池
//newFixedThreadPool() 参数为线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
//2.执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//3.关闭连接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}