Android面試筆記——生產(chǎn)者消費者模式

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ProducerAndConsumer {
    public static void main(String[] args) {
        //準(zhǔn)備15個線程,5個用于生產(chǎn)者,10個用于消費者
        ExecutorService executorService = Executors.newFixedThreadPool(15);
        ArrayList<Integer> list  = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            executorService.submit(new Producer(list,10));
        }
        for (int i = 0; i < 10; i++) {
            executorService.submit(new Consumer(list));
        }
    }


    public static class Producer implements Runnable{
        final ArrayList<Integer> list;
        //產(chǎn)品的最大數(shù)量
        int maxProductions;
        //在構(gòu)造函數(shù)中傳入列表
        public Producer(ArrayList<Integer> list, int maxProductions) {
            this.list = list;
            this.maxProductions = maxProductions;
        }


        @Override
        public void run() {
            //死循環(huán)
            while (true){
                //對list加鎖
                synchronized (list){
                    //判斷產(chǎn)品是否達(dá)到最大數(shù)量
                    //注意這里不要用if,要用while
                    //因為當(dāng)前線程被喚醒時,如果其他線程已經(jīng)率先完成生產(chǎn)(此時數(shù)量已經(jīng)達(dá)到最大),此線程再生產(chǎn)就超過了最大數(shù)量
                    while(list.size()==maxProductions){
                        System.out.println("生產(chǎn)者停止生產(chǎn),"+"產(chǎn)品數(shù)量:"+list.size());
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("生產(chǎn)者開始生產(chǎn),"+"產(chǎn)品數(shù)量:"+list.size());
                    }
                    list.add(1);
                    list.notifyAll();
                    System.out.println("生產(chǎn)者生產(chǎn)完成,"+"產(chǎn)品數(shù)量:"+list.size());
                }
            }
        }
    }
    public static class Consumer implements Runnable{
        final ArrayList<Integer> list;

        public Consumer(ArrayList<Integer> list) {
            this.list = list;
        }

        @Override
        public void run() {
            while (true){
                synchronized (list){
                    //判斷產(chǎn)品數(shù)量是否為0,同樣用while判斷
                    while(list.size()==0){
                        System.out.println("消費者停止消費,"+"產(chǎn)品數(shù)量:"+list.size());
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("消費者開始消費,"+"產(chǎn)品數(shù)量:"+list.size());
                    }
                    list.remove(0);
                    list.notifyAll();
                    System.out.println("消費者消費完成,"+"產(chǎn)品數(shù)量:"+list.size());
                }

            }
        }
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容