我正在尝试实现一个连接池,它有方法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方法是否是线程安全的。
答案 0 :(得分:0)
java.util.Queue
是一个接口,该接口的实现是否是线程安全的,取决于您选择的实现。
Oracle有一个小页面显示各种&#34;标准&#34;这里的队列实现:https://docs.oracle.com/javase/tutorial/collections/implementations/queue.html