1.`
while (true) {
OperationFuture<Boolean> future = client.add(key, lockSec, "");
if (future.isDone() && future.get()) { //加锁成功
return future.get();
}
long now = System.currentTimeMillis();
long costed = now - start;
if (costed > timeoutSec * 1000) { //超时, 加锁失败
return false;
}
}`
2.上面我想用add模拟一个加锁操作,但是控制报如下警告, 而且我发现第一个if判断条件始终进不去
2016-03-26 17:01:24.828 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/120.27.45.39:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-03-26 17:01:24.829 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/120.27.45.158:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2016-03-26 17:01:24.877 WARN net.spy.memcached.MemcachedConnection: Closing, and reopening {QA sa=/120.27.45.158:11211, #Rops=0, #Wops=265, #iq=4, topRop=null, topWop=Cmd: add Key: haoyinduo_lock1118 Flags: 0 Exp: 10 Data Length: 0, toWrite=0, interested=8}, attempt 1.
2016-03-26 17:01:24.878 WARN net.spy.memcached.MemcachedConnection: Closing, and reopening {QA sa=/120.27.45.39:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=8}, attempt 1.
2016-03-26 17:01:28.879 INFO net.spy.memcached.MemcachedConnection: Reconnecting {QA sa=/120.27.45.158:11211, #Rops=0, #Wops=103721, #iq=250, topRop=null, topWop=Cmd: add Key: haoyinduo_lock1118 Flags: 0 Exp: 10 Data Length: 0, toWrite=0, interested=0}
2016-03-26 17:01:28.880 WARN net.spy.memcached.MemcachedConnection: Closing, and reopening {QA sa=/120.27.45.158:11211, #Rops=0, #Wops=104677, #iq=213, topRop=null, topWop=Cmd: add Key: haoyinduo_lock1118 Flags: 0 Exp: 10 Data Length: 0, toWrite=0, interested=8}, attempt 2.
2016-03-26 17:01:28.880 INFO net.spy.memcached.MemcachedConnection: Reconnecting {QA sa=/120.27.45.39:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0}
2016-03-26 17:01:28.884 WARN net.spy.memcached.MemcachedConnection: Closing, and reopening {QA sa=/120.27.45.39:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=8}, attempt 2.
3.我改了一下第一个if判断,去掉了future.isDone() , 这下成功了
4.我的问题, 这个库提供的add方法应该是一个异步的通知。 我调用future.isDone(), 看看操作完成没有, 这是一个异步的操作, 如果操作完成了, 紧接着&& future.get() 这就能得到结果了, get是一个阻塞的操作. 我的想法哪里错了吗?
请大家帮忙看看
这个future.isDone不是一个阻塞操作,这个在主线程判断的时候futrue可能还么有完成。所以不会进行判断future.get()