开发者

Is LinkedBlockingQueue a correct choice for a producer-consumer like scenario?

开发者 https://www.devze.com 2023-01-03 07:12 出处:网络
I have a producer-consumer like scenario. Class A produces objects of type E. I have to hold开发者_开发知识库 it in a static data structure in class A, because the consumer logic should be handled in

I have a producer-consumer like scenario. Class A produces objects of type E. I have to hold开发者_开发知识库 it in a static data structure in class A, because the consumer logic should be handled in a class B, which has no reference to object of A. Is LinkedBlockingQueue the correct data type for the queue? Or are there any better selection for this?


If you are using a bounded queue, you could also consider an ArrayBlockingQueue.

Also, from the LinkedBlockingQueue Javadoc:

Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.


If the producer and consumer runs in two different threads, then yes, a LinkedBlockingQueue<E> would be a good choice.

0

精彩评论

暂无评论...
验证码 换一张
取 消