天天看點

Java多線程_生産者消費者模式

Java多線程_生産者消費者模式
Java多線程_生産者消費者模式
Java多線程_生産者消費者模式
Java多線程_生産者消費者模式
package com.kuangshen.gaoji;

//測試:生産者消費者模型 --> 利用緩沖區解決:管程法
//生産者,消費者,産品,緩沖區
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;
    }
}

//緩沖區
class SynContainer {
    //需要一個容器大小
    Chicken[] chickens = new Chicken[10];
    //容器計數器
    int count = 0;

    //生産者放入産品
    public synchronized void push(Chicken chicken) {
        //如果容器滿了,就需要等待消費者消費
        if (count == chickens.length) {
            //通知消費者消費,生産等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果沒有滿,我們就需要丢入産品
        chickens[count] = chicken;
        count++;
        //可以通知消費者消費了
        this.notifyAll();
    }

    //消費者消費産品
    public synchronized Chicken pop() {
        //判斷能否消費
        if (count == 0) {
            //等待生産者生産,消費者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果可以消費
        count--;
        Chicken chicken = chickens[count];
        //吃完了,通知生産者生産
        this.notifyAll();
        return chicken;
    }
}      
Java多線程_生産者消費者模式
package com.kuangshen.gaoji;

//測試生産者消費者問題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() {
        for (int i = 0; i < 20; i++) {
            if (i % 2 == 0) {
                this.tv.play("快樂大學營播放中");
            } else {
                this.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 voice; //表演的節目
    boolean flag = true;

    //表演
    public synchronized void play(String voice) {
        if (!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("演員表演了:" + voice);
        //通知觀衆觀看
        this.notifyAll();//通知喚醒
        this.voice = voice;
        this.flag = !this.flag;
    }

    //觀看
    public synchronized void watch() {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("觀看了:" + voice);
        //通知演員表演
        this.notifyAll();
        this.flag = !this.flag;
    }
}      
Java多線程_生産者消費者模式
Java多線程_生産者消費者模式
package com.kuangshen.gaoji;

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);
        //執行
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        //關閉連接配接
        service.shutdown();
    }
}

class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}