我爲我的應用程序使用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