Skip to content

Instantly share code, notes, and snippets.

View mattknox's full-sized avatar

matt knox mattknox

View GitHub Profile
@marcel
marcel / ConcurrentBlockingQueueConsumer.scala
Created March 24, 2011 22:43
Abstracts away the common pattern of producing items into a queue that are consumed concurrently by a pool of workers.
import java.util.concurrent.{BlockingQueue, ArrayBlockingQueue, CountDownLatch, Executors, TimeUnit}
/*
Abstracts away the common pattern of producing items into a queue that are
consumed concurrently by a pool of workers.
*/
class ConcurrentBlockingQueueConsumer[T](queue: BlockingQueue[T], producer: Iterator[T], concurrencyLevel: Int) {
lazy val stopLatch = new CountDownLatch(1)
lazy val pool = Executors.newFixedThreadPool(concurrencyLevel)