4.7多线程--线程协作

生产者消费者模式

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());
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在Typora上写完之后,发布到简书上,很多样式都变了 一、线程与进程 二、多线程创建方式 实现Runnable接...
    吴小贰阅读 1,405评论 0 1
  • 一、线程协作分析 二、线程协作模型-生产者消费者模型 三、解决-管程法 使用缓冲区 四、信号灯法 使用信号灯的真假
    StayHungriest阅读 1,549评论 0 0
  • 线程同步 并发:同一个对象被多个线程同时操作 处理多线程问题时,多个线程访问同一个对象,并且某些线程还想修改这个对...
    PeepSoul阅读 2,920评论 0 1
  • 多线程 以道路为例子:原来是一条路,慢慢因为车多了,道路阻塞,效率极低为了提高使用的效率,能够充分利用道路,于是加...
    关阿狸阅读 1,594评论 0 0
  • 线程协作 生产者和消费者问题假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库产品取走消费如...
    笨比乔治阅读 1,229评论 0 1

友情链接更多精彩内容