阻塞队列的性能对比

来源:互联网 发布:java返回值类引用 编辑:程序博客网 时间:2024/05/23 16:45

阻塞队列的性能对比

主要是比较offer跟poll两个方法的性能,开N个线程,每个线程往队列里写或者取500个整数。

线程数
20
50
100
200
5001000
LinkedBlockingQueue     
15,0
31,15
32,16
63,32
203,47
563,110
ArrayBlockingQueue15,0
16,15
31,15
47,16
125,47
364,68
PriorityBlockingQueue78,78
172,188
360,422
813,969
3094,2641
6547,5453


逗号前的数字是offer测试花费的时间,逗号后的数字是poll测试花费的时间。
结论:
1、ArrayBlockingQueue性能优于LinkedBlockingQueue,但是LinkedBlockingQueue是无界的。
2、ArrayBlockingQueue和LinkedBlockingQueue的poll方法总是比offer方法快,并发越高,差距越大
3、ArrayBlockingQueue和LinkedBlockingQueue的性能远高于PriorityBlockingQueue,显然优先队列在比较优先级上的操作上耗费太多
4、PriorityBlockingQueue的offer方法与poll方法的性能差距很小,基本维持在近似1:1