Producer/Consumer threads using a Queue

前端 未结 7 1763
抹茶落季
抹茶落季 2020-11-22 17:00

I\'d like to create some sort of Producer/Consumer threading app. But I\'m not sure what the best way to implement a queue between the two.

So I\'ve so

7条回答
  •  臣服心动
    2020-11-22 18:03

    This is a very simple code.

    import java.util.*;
    
    // @author : rootTraveller, June 2017
    
    class ProducerConsumer {
        public static void main(String[] args) throws Exception {
            Queue queue = new LinkedList<>();
            Integer buffer = new Integer(10);  //Important buffer or queue size, change as per need.
    
            Producer producerThread = new Producer(queue, buffer, "PRODUCER");
            Consumer consumerThread = new Consumer(queue, buffer, "CONSUMER");
    
            producerThread.start();  
            consumerThread.start();
        }   
    }
    
    class Producer extends Thread {
        private Queue queue;
        private int queueSize ;
    
        public Producer (Queue queueIn, int queueSizeIn, String ThreadName){
            super(ThreadName);
            this.queue = queueIn;
            this.queueSize = queueSizeIn;
        }
    
        public void run() {
            while(true){
                synchronized (queue) {
                    while(queue.size() == queueSize){
                        System.out.println(Thread.currentThread().getName() + " FULL         : waiting...\n");
                        try{
                            queue.wait();   //Important
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
    
                    //queue empty then produce one, add and notify  
                    int randomInt = new Random().nextInt(); 
                    System.out.println(Thread.currentThread().getName() + " producing... : " + randomInt); 
                    queue.add(randomInt); 
                    queue.notifyAll();  //Important
                } //synchronized ends here : NOTE
            }
        }
    }
    
    class Consumer extends Thread {
        private Queue queue;
        private int queueSize;
    
        public Consumer(Queue queueIn, int queueSizeIn, String ThreadName){
            super (ThreadName);
            this.queue = queueIn;
            this.queueSize = queueSizeIn;
        }
    
        public void run() {
            while(true){
                synchronized (queue) {
                    while(queue.isEmpty()){
                        System.out.println(Thread.currentThread().getName() + " Empty        : waiting...\n");
                        try {
                            queue.wait();  //Important
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
    
                    //queue not empty then consume one and notify
                    System.out.println(Thread.currentThread().getName() + " consuming... : " + queue.remove());
                    queue.notifyAll();
                } //synchronized ends here : NOTE
            }
        }
    }
    

提交回复
热议问题