这是生产者/消费者的经典问题。当我引导我的Spring Boot应用程序时,我同时启动了两个线程。我只想在收到httpRequest时从Producer线程写入共享队列。那么,如何将这个值传递给我的Producer线程,以便可以将其放入共享队列中?可能吗?

主班

public class ProducerConsumerPattern {
    public static void main(String args[]) {

        // create shared object
        BlockingQueue sharedQueue = new LinkedBlockingQueue();

        // create Producer and Consumer Thread
        Thread prodThread = new Thread(new Producer(sharedQueue));
        Thread consThread = new Thread(new Consumer(sharedQueue));

        // start producer and Consumer thread
        prodThread.start();
        consThread.start();
    }
}


消费阶层

class Consumer implements Runnable {
    private final BlockingQueue sharedQueue;

    public Consumer (BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    @Override
    public void run() {
        while(true) {
            try {
                System.out.println("Consumed: "+ sharedQueue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


生产者阶层

class Producer implements Runnable {
    private final BlockingQueue sharedQueue;

    public Producer(BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    @Override
    public void run() {
        // I don't want to write in the queue the counter values.
        // I want to put my own values, when I receive them from outside **
        for (int i=0; i<10; i++) {
            try {
                System.out.println("Produced: " + i);
                sharedQueue.put(i);
            } catch (InterruptedException ex) {
                Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


我可以通过@RestController@RequestMapping获取http参数,但是如何获取Producer线程并将此新值放入队列?

提前致谢!

最佳答案

您需要获得生产者的处理才能将任何项目推入队列。在生产者中编写一个方法来推送项目:

public class Producer implements Runnable {
    private final BlockingQueue sharedQueue;

    public Producer(BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    public void pushItem(int item) throws InterruptedException {
        System.out.println("Produced: " + item);
        sharedQueue.put(item);
    }

    @Override
    public void run() {
        //I don't want to write in the queue the counter values. I want to put my own values, when I receive them from outside **
        for(int i=0; i<10; i++){
            try {
                System.out.println("Produced: " + i);
                sharedQueue.put(i);
            } catch (InterruptedException ex) {
                //Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


现在在main方法中,您需要编写:

public static void main(String[] args) throws InterruptedException {
        //Creating shared object
        BlockingQueue sharedQueue = new LinkedBlockingQueue();

        //Creating Producer and Consumer Thread
        Producer producer = new Producer(sharedQueue);
        Thread prodThread = new Thread(producer);
        Thread consThread = new Thread(new Consumer(sharedQueue));

        //Starting producer and Consumer thread
        prodThread.start();
        consThread.start();

        producer.pushItem(2000);
    }


项目2000将由主线程推送,但不能保证顺序。样本运行的输出为:

Produced: 0Produced: 2000Produced: 1Produced: 2Produced: 3Consumed: 0Produced: 4Consumed: 2000Produced: 5Produced: 6Consumed: 1Produced: 7Consumed: 2Produced: 8Produced: 9Consumed: 3Consumed: 4Consumed: 5Consumed: 6Consumed: 7Consumed: 8Consumed: 9

08-19 10:53