我为我的应用程序使用LinkedBlockingQueue进行隐式同步,但如果我使用queue.take()
或queue.poll()
,前几个元素在从队列中获取后不知何故丢失。我已经检查过它是否是同一个对象。LinkedBlockingQueue第一个元素丢失
这里是我的代码:
for (QueryResult result : tmpPage) {
String objectId = result.getPropertyValueByQueryName("cmis:objectId");
writer.writeFile(objectId); //Only for debugging reasons to
//compare the input and the output
try {
batchJobs.offer(new Node(objectId), 1,TimeUnit.HOURS);
} catch(Exception e) {
errorLogger.error(e.getMessage());
}
}
,我采取或轮询
Node node = null;
while (!nodes.isEmpty()) {
while((node = nodes.take())!=null) {
writer.writeFile(node.getObjectID()); // Only for debugging reasons
if (node != null) {
//Do some stuff
}
}
}
曾有人遇到过类似的事情的地方吗?
你可以澄清“第一个从队列中获得它之后很少有元素会丢失“?你的意思是说,从队列中移除元素后,它不再可用?顺便说一下,'if(node!= null)'是不需要的。你在内部'while'循环的条件下保证'node!= null'。 – bradimus