producer/consumer work queues

前端 未结 3 972
眼角桃花
眼角桃花 2020-12-08 03:46

I\'m wrestling with the best way to implement my processing pipeline.

My producers feed work to a BlockingQueue. On the consumer side, I poll the queue, wrap what I

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 04:10

    I want the shared work queue to represent the current processing state.

    Try using a shared BlockingQueue and have a pool of Worker threads taking work items off of the Queue.

    I want to be able to block the producers if the consumers aren't keeping up.

    Both ArrayBlockingQueue and LinkedBlockingQueue support bounded queues such that they will block on put when full. Using the blocking put() methods ensures that producers are blocked if the queue is full.

    Here is a rough start. You can tune the number of workers and queue size:

    public class WorkerTest {
    
        private final BlockingQueue workQueue;
        private final ExecutorService service;
    
        public WorkerTest(int numWorkers, int workQueueSize) {
            workQueue = new LinkedBlockingQueue(workQueueSize);
            service = Executors.newFixedThreadPool(numWorkers);
    
            for (int i=0; i < numWorkers; i++) {
                service.submit(new Worker(workQueue));
            }
        }
    
        public void produce(T item) {
            try {
                workQueue.put(item);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    
    
        private static class Worker implements Runnable {
            private final BlockingQueue workQueue;
    
            public Worker(BlockingQueue workQueue) {
                this.workQueue = workQueue;
            }
    
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        T item = workQueue.take();
                        // Process item
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
            }
        }
    }
    

提交回复
热议问题