并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较.docx
- 文档编号:29022232
- 上传时间:2023-07-20
- 格式:DOCX
- 页数:9
- 大小:15.60KB
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较.docx
《并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较.docx》由会员分享,可在线阅读,更多相关《并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较.docx(9页珍藏版)》请在冰豆网上搜索。
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较
2011-01-1908:
522802人阅读
测试结果显示:
阻塞队列和并发队列在高并发情况下,性能相差不大。
1Test.java代码参考viewplaincopyprint?
publicclassConcurrentQueueTest{
privatestaticintCOUNT=100000;
privatestaticintTHREAD_NUM=10;
privatestaticCyclicBarrierThreadcyclicBarrierThread=newCyclicBarrierThread();
privatestaticConcurrentLinkedQueueconQueue=newConcurrentLinkedQueue();
privatestaticLinkedBlockingQueuelinkQueue=newLinkedBlockingQueue();
staticclassConcurrentLinkedQueueProducerextendsTest{
publicConcurrentLinkedQueueProducer(Stringid,CyclicBarrierbarrier,
longcount,intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}
@Override
protectedvoidtest(){
conQueue.add
(1);
}
}
staticclassLinkedBlockingQueueProducerextendsTest{
publicLinkedBlockingQueueProducer(Stringid,CyclicBarrierbarrier,
longcount,intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}
@Override
protectedvoidtest(){
conQueue.add
(1);
}
}
staticclassCyclicBarrierThreadextendsThread{
@Override
publicvoidrun(){
conQueue.clear();
linkQueue.clear();
}
}
publicstaticvoidtest(Stringid,longcount,intthreadNum,
ExecutorServiceexecutor){
finalCyclicBarrierbarrier=newCyclicBarrier(threadNum+1,
cyclicBarrierThread);
System.out.println("==============================");
System.out.println("count="+count+"/t"+"ThreadCount="
+threadNum);
concurrentTotalTime+=newConcurrentLinkedQueueProducer(
"ConcurrentLinkedQueueProducer",barrier,COUNT,threadNum,
executor).startTest();
linkedBlockingTotalTime+=newLinkedBlockingQueueProducer(
"LinkedBlockingQueueProducer",barrier,COUNT,threadNum,
executor).startTest();
totalThreadCount+=threadNum;
executor.shutdownNow();
System.out.println("==============================");
}
staticlongconcurrentTotalTime=0;
staticlonglinkedBlockingTotalTime=0;
staticlongtotalThreadCount=0;
publicstaticvoidmain(String[]args)throwsInterruptedException{
for(inti=1;i20;i++){
ExecutorServiceexecutor=Executors.newFixedThreadPool(THREAD_NUM
*i);
test("",COUNT,10*i,executor);
}
System.out.println("ConcurrentLinkedQueueAvgTime="
+concurrentTotalTime/totalThreadCount);
System.out.println("LinkedBlockingQueueAvgTime="
+linkedBlockingTotalTime/totalThreadCount);
}
}publicclassConcurrentQueueTest{
privatestaticintCOUNT=100000;
privatestaticintTHREAD_NUM=10;
privatestaticCyclicBarrierThreadcyclicBarrierThread=newCyclicBarrierThread();privatestaticConcurrentLinkedQueueconQueue=newConcurrentLinkedQueue();
privatestaticLinkedBlockingQueuelinkQueue=newLinkedBlockingQueue();staticclassConcurrentLinkedQueueProducerextendsTest{publicConcurrentLinkedQueueProducer(Stringid,CyclicBarrierbarrier,
longcount,intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}@Override
protectedvoidtest(){
conQueue.add
(1);
}
}staticclassLinkedBlockingQueueProducerextendsTest{publicLinkedBlockingQueueProducer(Stringid,CyclicBarrierbarrier,
longcount,intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}@Override
protectedvoidtest(){
conQueue.add
(1);
}
}staticclassCyclicBarrierThreadextendsThread{
@Override
publicvoidrun(){
conQueue.clear();
linkQueue.clear();
}
}publicstaticvoidtest(Stringid,longcount,intthreadNum,
ExecutorServiceexecutor){finalCyclicBarrierbarrier=newCyclicBarrier(threadNum+1,
cyclicBarrierThread);System.out.println("==============================");
System.out.println("count="+count+"/t"+"ThreadCount="
+threadNum);concurrentTotalTime+=newConcurrentLinkedQueueProducer(
"ConcurrentLinkedQueueProducer",barrier,COUNT,threadNum,
executor).startTest();
linkedBlockingTotalTime+=newLinkedBlockingQueueProducer(
"LinkedBlockingQueueProducer",barrier,COUNT,threadNum,
executor).startTest();totalThreadCount+=threadNum;
executor.shutdownNow();System.out.println("==============================");
}staticlongconcurrentTotalTime=0;
staticlonglinkedBlockingTotalTime=0;staticlongtotalThreadCount=0;publicstaticvoidmain(String[]args)throwsInterruptedException{
for(inti=1;i<20;i++){
ExecutorServiceexecutor=Executors.newFixedThreadPool(THREAD_NUM
*i);
test("",COUNT,10*i,executor);
}System.out.println("ConcurrentLinkedQueueAvgTime="
+concurrentTotalTime/totalThreadCount);System.out.println("LinkedBlockingQueueAvgTime="
+linkedBlockingTotalTime/totalThreadCount);
}
}结果,执行100,000次的并发入队操作,并发队列需要49毫秒,阻塞队列需要53毫秒,比阻塞队列平均快4毫秒。
这种平均值的算法可能不准确,因为随着线程数量的增加,线程之间切换的开销会逐渐增大。
[c-sharp]viewplaincopyprint?
==============================
count=100000ThreadCount=10
ConcurrentLinkedQueueProducer=422
LinkedBlockingQueueProducer=390
==============================
==============================
count=100000ThreadCount=20
ConcurrentLinkedQueueProducer=782
LinkedBlockingQueueProducer=828
==============================
==============================
count=100000ThreadCount=30
ConcurrentLinkedQueueProducer=1468
LinkedBlockingQueueProducer=1422
==============================
==============================
count=100000ThreadCount=40
ConcurrentLinkedQueueProducer=2188
LinkedBlockingQueueProducer=1297
==============================
==============================
count=100000ThreadCount=50
ConcurrentLinkedQueueProducer=2609
LinkedBlockingQueueProducer=2625
==============================
==============================
count=100000ThreadCount=60
ConcurrentLinkedQueueProducer=3078
LinkedBlockingQueueProducer=2969
==============================
==============================
count=100000ThreadCount=70
ConcurrentLinkedQueueProducer=4046
LinkedBlockingQueueProducer=3594
==============================
==============================
count=100000ThreadCount=80
ConcurrentLinkedQueueProducer=4047
LinkedBlockingQueueProducer=4078
==============================
==============================
count=100000ThreadCount=90
ConcurrentLinkedQueueProducer=4469
LinkedBlockingQueueProducer=5109
==============================
==============================
count=100000ThreadCount=100
ConcurrentLinkedQueueProducer=5125
LinkedBlockingQueueProducer=4922
==============================
==============================
count=100000ThreadCount=110
ConcurrentLinkedQueueProducer=5531
LinkedBlockingQueueProducer=5969
==============================
==============================
count=100000ThreadCount=120
ConcurrentLinkedQueueProducer=5172
LinkedBlockingQueueProducer=6188
==============================
==============================
count=100000ThreadCount=130
ConcurrentLinkedQueueProducer=7187
LinkedBlockingQueueProducer=6594
==============================
==============================
count=100000ThreadCount=140
ConcurrentLinkedQueueProducer=6937
LinkedBlockingQueueProducer=7782
==============================
==============================
count=100000ThreadCount=150
ConcurrentLinkedQueueProducer=6921
LinkedBlockingQueueProducer=8875
==============================
==============================
count=100000ThreadCount=160
ConcurrentLinkedQueueProducer=8047
LinkedBlockingQueueProducer=9250
==============================
==============================
count=100000ThreadCount=170
ConcurrentLinkedQueueProducer=8235
LinkedBlockingQueueProducer=9656
==============================
==============================
count=100000ThreadCount=180
ConcurrentLinkedQueueProducer=9062
LinkedBlockingQueueProducer=9719
==============================
==============================
count=100000ThreadCount=190
ConcurrentLinkedQueueProducer=9422
LinkedBlockingQueueProducer=9609
==============================
ConcurrentLinkedQueueAvgTime=49
LinkedBlockingQueueAvgTime=53
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 并发 队列 ConcurrentLinkedQueue 阻塞 LinkedBlockingQueue 入队 操作 性能 比较
链接地址:https://www.bdocx.com/doc/29022232.html