java.util.Queue轮询和add方法是原子/线程安全的吗

java.util.Queue轮询和add方法是原子/线程安全的吗,java,multithreading,Java,Multithreading,我正在尝试实现一个连接池,它有getFromPool和returnToPool方法 private java.util.Queue<XXXConnection> xxxConnectionQueue; public XXXConnection get() { XXXConnection xxxConnection = null; if (semaphore.tryAcquire()) { confServerProtocol

我正在尝试实现一个连接池,它有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();                
        }
    }
}
private java.util.Queue xxxConnectionQueue;
public XXXConnection get(){
XXXConnection XXXConnection=null;
if(semaphore.tryAcquire()){
confServerProtocol=configServerConnectionQueue.poll();
}
返回协议;
}
受保护的拓扑(XXX连接XXX连接){
if(xxxConnectionValidator.isValid(xxxConnection)){
if(xxxConnectionQueue.add(xxxConnection)){
semaphore.release();
}
}
}
在这里,XXXconnectionValidator在将连接返回到池之前检查连接是否有效。
想要确认java.util.Queue的add和poll方法是否是线程安全的。

java.util.Queue
是一个接口,该接口的实现是否是线程安全的取决于您选择的实现

Oracle有一个小页面,其中显示了各种“标准”队列实现: