生产者和消费者共享队列,值来自 HttpRequest

Producer and Consumer shared queue with values coming from HttpRequest

这是Producer/Consumer的经典问题。当我 bootstrap 我的 Spring 引导应用程序时,我启动了两个线程。当我收到 httpRequest 时,我只想从生产者线程写入共享队列。那么,我怎样才能将这个值传递给我的生产者线程,以便我可以将它放入共享队列中呢?可能吗?

主要Class

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

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

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线程并将这个新值放入队列中呢?

提前致谢!

您需要获取生产者句柄才能将任何项目推送到队列。在producer中写一个推送item的方法:

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: 0 Produced: 2000 Produced: 1 Produced: 2 Produced: 3 Consumed: 0 Produced: 4 Consumed: 2000 Produced: 5 Produced: 6 Consumed: 1 Produced: 7 Consumed: 2 Produced: 8 Produced: 9 Consumed: 3 Consumed: 4 Consumed: 5 Consumed: 6 Consumed: 7 Consumed: 8 Consumed: 9