java 有没有一种有固定大小的并发队列,在有新的数据插入的队尾的时候自动poll掉队头的元素?
PHP中文网
PHP中文网 2017-04-17 11:46:08
0
3
681

java 有没有一种有固定大小的并发队列,在有新的元素插入的队尾的时候能自动判断如果队列满了就poll掉队头的元素,没满就offer进去?

ps:考虑过LinkedBlockingQueue和ConcurrentLinkedQueue

ConcurrentLinkedQueue可以用size()判断大小
LinkedBlockingQueue可以用offer()判断,如果塞不进去说明队列满了

但是效率都不太高(在判断队列是否满了的情况。。。)
而且由于E元素是业务bean,比较大,所以100并发时候大概到了300~400ms。。。。

求大神更优的解决方案。。。

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
左手右手慢动作
  1. Why do all 100 concurrencies need to be concentrated in this Queue? There seems to be something wrong with the design
  2. Solution: RingBuffer. Please refer to wikipedia for details. Very efficient
巴扎黑

ArrayBlockingQueue should meet the requirements of LZ, but you still need to judge based on the return value of the offer.

The size of ConcurrentLinkedQueue is not a constant time operation, so it is slow. LinkedBlockingQueue is implemented using a linked list, and it is not specifically designed for fixed-size queues, so it is also slow.

黄舟

Easy, ArrayBlockingQueue.

If it is more complicated, especially if the reading thread is much larger than the writing thread, use LMAX RingBuffer.

LinkedQueueSlowness is because every time you insert or delete an element, there are four pointer operations. If you have a fixed size, using array is the best way.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template