1

这个问题与 com.jayway.awaitility.Awaitility 有关。

我刚刚尝试了 Awaitility.await() ,它似乎有一些奇怪的行为。在下面的测试方法中,如果我注释掉 testWithFuture() 并启用 testWithAwaitility(),我永远不会看到打印出消息“结束”。我看到“开始”,然后程序就退出了,第二个打印语句似乎永远不会到达。

因此,作为一种变通方法,我决定使用 Settable{Future}.. 如果其他人有同样的问题,那么我提供的变通方法可能会很有用.. 更好的是得到一个好的答案;^)!在此先感谢/克里斯

编码:


import com.google.common.util.concurrent.SettableFuture;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.jayway.awaitility.Awaitility.await;
import static java.util.concurrent.TimeUnit.SECONDS;

public class AwaitTest {
    static volatile boolean done = false;

    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
        testWithFuture();
        //testWithAwaitility();
    }

    private static void testWithAwaitility() {
        System.out.println("start " + new Date());
        new Thread(new Runnable(){
            public void run(){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                done = true;
            }

        }).start();


        await().atMost(2, SECONDS).until(new Callable() {
            @Override
            public Boolean call() throws Exception {
                return done;
            }
        });

        System.out.println("end " + new Date());   // NEVER Reached. i wonder why?

    }

    // This does what I want.
    //
    private static void testWithFuture() throws InterruptedException, ExecutionException, TimeoutException {
        System.out.println("start testWithFuture");

        final SettableFuture future = SettableFuture. create();
        new Thread(new Runnable(){

            public void run(){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                future.set("Hello");
            }

        }).start();

        String result = future.get(4, TimeUnit.SECONDS);

        if (! result.equals("Hello")) {
            throw new RuntimeException("not equal");
        } else {
            System.out.println("got Hello");

        }
    }
 }

更正的代码->

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.jayway.awaitility.Awaitility.await;
import static java.util.concurrent.TimeUnit.SECONDS;

public class Sample {
    static volatile boolean done = false;

    public static void main(String[] args) {
        testWithAwaitility();
    }

    private static void testWithAwaitility() {
        System.out.println("start " + new Date());
        new Thread(new Runnable(){
            public void run(){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                done = true;
            }

        }).start();


        try {
            await().atMost(2, SECONDS).until(new Callable() {
                @Override
                public Boolean call() throws Exception {
                    return done;
                }
            });
        } catch (Exception e) {
            System.out.println("FAILED");
            e.printStackTrace();
        }


        System.out.println("end " + new Date());   // REACHED this statement after correction
    }
 }
4

1 回答 1

5

根据文档,如果达到超时且条件不成立,则await()抛出 a TimeoutException,因此您的方法在此时结束,因为异常通过堆栈向上传播。这解释了这种行为。但是,您应该会看到堆栈跟踪。

如果您想在之后继续执行代码,您似乎需要捕获此异常。

于 2013-10-28T06:14:46.320 回答