我有下一个代码:
LinkedBlockingQueue< String > messages = new LinkedBlockingQueue< >( INITIALCAPACITY );
//current thread calls
private synchronized String waitMessages() throws InterruptedException {
while( messages.isEmpty() ) {
wait();
}
return messages.remove( );
}
//another thread calls
public synchronized void received( String message ) {
messages.add( message );
notify();
}因此,我运行这段代码,为LinkedBlockingQueue和LinkedList运行相同的输入数据,我发现第一个实现比LinkedList执行得更快。这对我来说是一种奇怪的行为,因为我认为LinkedBlockingQueue需要额外的时间来进行内部阻塞机制。
( 1)你能解释一下这种结果吗?
( 2)是否有任何队列(或列表)实现比LinkedBlockingQueue (ArrayList绝对慢)更快?
发布于 2016-03-14 10:36:45
因此,第一个问题可以这样回答: LinkedBlockingQueue在init阶段(构造函数)分配内存。但是LinkedList在运行时阶段执行每个insert操作。
https://stackoverflow.com/questions/35984955
复制相似问题