未来的行为。得到与0超时

时间:2022-04-08 20:58:25

Can anyone point me to some documentation that makes clear that a 'Future.get` with a timeout of 0 will not wait?

谁能给我指出一些文件来说明“未来”。得到“超时为0将不会等待?”

The API docs for java.util.concurrent.Future does not make explicit the behavior of future.get(0, unit). Standing on its own, the statement "Waits if necessary for at most the given time..." implies this invocation will not wait at all, but given the long-standing behavior of Object.wait(0) (infinite wait), I'm nervous to depend on a "no wait" behavior of future.get(0, unit)

用于java.util.concurrent的API文档。未来并不明确表明未来的行为。(0,单元)。站在自己的立场上,“如果需要的话,最多在给定的时间内等待……”意味着这个调用根本不会等待,但考虑到object的长期行为。get(0,单位)

Scanning the source of some JDK-provided classes (viz. FutureTask) I see that this particular implementation of Future does not wait when the timeout is 0.

扫描一些jdk提供的类(即FutureTask)的源代码,我发现这个Future实现不会在超时为0时等待。

I'd like to be able to say

我想说

   long timeout = Math.max(until - now, 0);
   return future.get(timeout, TimeUnit.MILLISECONDS);

but I'm nervous about a Future implementing that as an infinite wait, so instead, I've explicitly coded it the way I would expect it to work:

但是我很担心将来会把它作为一个无限的等待,所以,我已经明确地将它编码成我期望它工作的方式:

   long timeout = Math.max(until - now, 0);
   if(timeout > 0 || future.isDone()){
      return future.get(timeout, TimeUnit.MILLISECONDS);
   } else {
      throw TimeoutException();
   }

2 个解决方案

#1


8  

Waits if necessary for at most the given time…

如果需要的话,最多等待给定的时间……

Waiting for at most zero time units is not waiting at all. That's not an implicit hint, it's an explicit guarantee.

等待最多是零时间单位根本不等待。这不是暗示,这是明确的保证。

#2


6  

Can anyone point me to some documentation that makes clear that a 'Future.get` with a timeout of 0 will not wait?

谁能给我指出一些文件来说明“未来”。得到“超时为0将不会等待?”

I can point you at some code if that helps. Looking into java.util.concurrent.FutureTask and then on to AbstractQueuedSynchronizer I see the following loop which I've pared down to show the relavent bits:

如果有用的话,我可以给你指出一些代码。调查java . util . concurrent。FutureTask,然后到AbstractQueuedSynchronizer,我看到下面的循环,我将它简化为显示相关的比特:

private boolean doAcquireSharedNanos(int arg, long nanosTimeout) {
    long lastTime = System.nanoTime();
    for (;;) {
        ...
        if (nanosTimeout <= 0) {
            cancelAcquire(node);
            return false;
        }
        long now = System.nanoTime();
        nanosTimeout -= now - lastTime;
    }

This means that if nanosTimeout is 0 (which it will be if you pass in 0 to get) then it will try to acquire the future once and then timeout and return false.

这意味着如果nanosTimeout是0(如果您通过0来获得),那么它将尝试一次获取一次,然后超时并返回false。

If it makes you feel any better, you can set your timeout to be 1 nanosecond.

如果它让你感觉更好,你可以把你的超时设置为1纳秒。

#1


8  

Waits if necessary for at most the given time…

如果需要的话,最多等待给定的时间……

Waiting for at most zero time units is not waiting at all. That's not an implicit hint, it's an explicit guarantee.

等待最多是零时间单位根本不等待。这不是暗示,这是明确的保证。

#2


6  

Can anyone point me to some documentation that makes clear that a 'Future.get` with a timeout of 0 will not wait?

谁能给我指出一些文件来说明“未来”。得到“超时为0将不会等待?”

I can point you at some code if that helps. Looking into java.util.concurrent.FutureTask and then on to AbstractQueuedSynchronizer I see the following loop which I've pared down to show the relavent bits:

如果有用的话,我可以给你指出一些代码。调查java . util . concurrent。FutureTask,然后到AbstractQueuedSynchronizer,我看到下面的循环,我将它简化为显示相关的比特:

private boolean doAcquireSharedNanos(int arg, long nanosTimeout) {
    long lastTime = System.nanoTime();
    for (;;) {
        ...
        if (nanosTimeout <= 0) {
            cancelAcquire(node);
            return false;
        }
        long now = System.nanoTime();
        nanosTimeout -= now - lastTime;
    }

This means that if nanosTimeout is 0 (which it will be if you pass in 0 to get) then it will try to acquire the future once and then timeout and return false.

这意味着如果nanosTimeout是0(如果您通过0来获得),那么它将尝试一次获取一次,然后超时并返回false。

If it makes you feel any better, you can set your timeout to be 1 nanosecond.

如果它让你感觉更好,你可以把你的超时设置为1纳秒。