这个问题与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
}
}
最佳答案
根据documentation,如果达到超时且条件不成立,则await()
会引发TimeoutException
,因此您的方法在此结束,因为异常是通过堆栈向上传播的。这解释了行为。但是,您应该看到一个堆栈跟踪。
如果要在以后继续执行代码,则似乎需要捕获此异常。
关于java - 如果超时间隔到期,Awaitility.await()。atMost(…)是否导致程序退出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19627335/