在memcached的java客户端中,
SockIOPool是对外服务的class,
SchoonerSockIOPool是真正保存缓存连接的class
对于这个2个对象,虽然构造函数都是protected的,但是使用方式却不同:
SockIOPool中:
public static synchronized SockIOPool getInstance(String poolName) {
SockIOPool whalinSockIOPool = new SockIOPool();
whalinSockIOPool.schoonerSockIOPool = SchoonerSockIOPool.getInstance(poolName);
return whalinSockIOPool;
}
每次都是创建一个新的SockIOPool对象;
而SchoonerSockIOPool.getInstance呢:
public static SchoonerSockIOPool getInstance(String poolName) {
SchoonerSockIOPool pool;
synchronized (pools) {
if (!pools.containsKey(poolName)) {
pool = new SchoonerSockIOPool(true);
pools.putIfAbsent(poolName, pool);
}
}
return pools.get(poolName);
}
pools是一个static的成员变量
private static ConcurrentMap<String, SchoonerSockIOPool> pools = new ConcurrentHashMap<String, SchoonerSockIOPool>();
因此如果poolName相同,无论创建了几个SockIOPool,SchoonerSockIOPool都是相同的;
而且同一个server(ip:port),tcp/udp的属性要是一致的,如果前后2次得到的tcp/udp属性不一致,后一次getInstance失败,得到的是null;