![](/img/trans.png)
[英]how can i call java.util.queue poll() method and not remove elements in my queue
[英]Are java.util.Queue poll and add methods Atomic/thread safe
我正在嘗試實現一個連接池,它具有方法getFromPool和returnToPool
private java.util.Queue<XXXConnection> xxxConnectionQueue;
public XXXConnection get() {
XXXConnection xxxConnection = null;
if (semaphore.tryAcquire()) {
confServerProtocol = configServerConnectionQueue.poll();
}
return confServerProtocol;
}
protected void returnToPool(XXXConnection xxxConnection) {
if (xxxConnectionValidator.isValid(xxxConnection)) {
if(xxxConnectionQueue.add(xxxConnection)) {
semaphore.release();
}
}
}
在此xxxConnectionValidator在將連接返回到池之前檢查連接是否為有效連接。 想要確認java.util.Queue的add和poll方法是否是線程安全的。
java.util.Queue
是一個接口,該接口的實現是否是線程安全的取決於您選擇的實現。
Oracle在此處有一小頁顯示各種“標准”隊列實現: https : //docs.oracle.com/javase/tutorial/collections/implementations/queue.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.