java 有没有一种有固定大小的并发队列,在有新的元素插入的队尾的时候能自动判断如果队列满了就poll掉队头的元素,没满就offer进去?
ps:考虑过LinkedBlockingQueue
ConcurrentLinkedQueue
LinkedBlockingQueue
但是效率都不太高(在判断队列是否满了的情况。。。)
而且由于E元素是业务bean,比较大,所以100并发时候大概到了300~400ms。。。。
求大神更优的解决方案。。。
ringa_lee2017-04-17 11:48:08
巴扎黑2017-04-17 11:48:08
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.
黄舟2017-04-17 11:48:08
Easy, ArrayBlockingQueue
.
If it is more complicated, especially if the reading thread is much larger than the writing thread, use LMAX RingBuffer
.
LinkedQueue
Slowness 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.