suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java - spymemcached的一个api问题?

1.`

1

2

3

4

5

6

7

8

9

10

11

12

<code>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;

            }

        }`

        </code>

2.上面我想用add模拟一个加锁操作,但是控制报如下警告, 而且我发现第一个if判断条件始终进不去

1

2

3

4

5

6

7

8

<code>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.</code>

3.我改了一下第一个if判断,去掉了future.isDone() , 这下成功了

4.我的问题, 这个库提供的add方法应该是一个异步的通知。 我调用future.isDone(), 看看操作完成没有, 这是一个异步的操作, 如果操作完成了, 紧接着&& future.get() 这就能得到结果了, get是一个阻塞的操作. 我的想法哪里错了吗?

请大家帮忙看看

PHP中文网PHP中文网2887 Tage vor326

Antworte allen(1)Ich werde antworten

  • 天蓬老师

    天蓬老师2017-04-17 17:40:14

    1

    2

    <code>if (future.isDone() && future.get()) {  //加锁成功

    </code>

    这个future.isDone不是一个阻塞操作,这个在主线程判断的时候futrue可能还么有完成。所以不会进行判断future.get()

    Antwort
    0
  • StornierenAntwort