【JAVA】 實現消費者生產者

通過wait()和 notifyAll()實現

public class PublicQueue {

    public int putindex = 0;
    public int maxcount = 10;

    public synchronized void add(){

        while(putindex >= maxcount){
            try {
                System.out.println("沒有東西可以加了");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        notifyAll();
        putindex++;
        System.out.println(Thread.currentThread().getName() + " 生產加1變成:"+putindex);

    }

    public synchronized void remove(){
        while(putindex == 0) {
            try {
                System.out.println("沒有東西可以減了");
                wait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        notifyAll();
        putindex--;
        System.out.println(Thread.currentThread().getName()+" 消費減1:"+putindex );

    }

}
public class Consumer implements Runnable {
    private PublicQueue publicQueue;

    public Consumer(PublicQueue publicQueue){
        this.publicQueue = publicQueue;
    }


    @Override
    public  void run(){
        while (true){
            try {
                Thread.sleep(3000);
                publicQueue.remove();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}

public class Producer implements Runnable {

    private PublicQueue publicQueue;

    public Producer(PublicQueue publicQueue){
        this.publicQueue = publicQueue;
    }


    @Override
    public  void run(){
        while (true){
            try {
                Thread.sleep(1000);
                publicQueue.add();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }


}
    public static void main(String[] args) {
        PublicQueue publicQueue = new PublicQueue();
        Thread p1 = new Thread(new Producer(publicQueue), "produce1");
        Thread p2 = new Thread(new Producer(publicQueue), "produce2");
        Thread p3 = new Thread(new Producer(publicQueue), "produce3");
        Thread s1 = new Thread(new Consumer(publicQueue), "consumer1");
        Thread s2 = new Thread(new Consumer(publicQueue), "consumer2");
        Thread s3 = new Thread(new Consumer(publicQueue), "consumer3");
        p1.start();
        p2.start();
        p3.start();
        s1.start();
        s2.start();
        s3.start();
    }

BlockingQueue 實現

public class BlockingQueueTest {

    public static  int count = 0;
    public BlockingQueue blockingQueue = new ArrayBlockingQueue(10);


    public static void main(String[] args) {
        BlockingQueueTest test3 = new BlockingQueueTest();
        new Thread(test3.new ProducerTest()).start();
        new Thread(test3.new ConsumerTest()).start();
        new Thread(test3.new ProducerTest()).start();
        new Thread(test3.new ConsumerTest()).start();
        new Thread(test3.new ProducerTest()).start();
        new Thread(test3.new ConsumerTest()).start();
        new Thread(test3.new ProducerTest()).start();
        new Thread(test3.new ConsumerTest()).start();
    }


    class ProducerTest implements  Runnable{

        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    blockingQueue.put(1);//add會拋出異常 put會阻塞
                    count++;
                    System.out.println(Thread.currentThread().getName()
                            + "生產者生產,目前總共有" + count);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }
    }
    class ConsumerTest implements  Runnable{

        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                try {
                    blockingQueue.take();
                    count--;
                    System.out.println(Thread.currentThread().getName()
                            + "消費者消費,目前總共有" + count);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }



            }
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章