【发布时间】:2020-09-01 21:43:05
【问题描述】:
以下代码的问题是,如果我删除 Thread.sleep(1000);,则会得到不一致的结果。我不想使用Thread.Sleep(1000);
由于我正在使用 vertx 的异步操作,所以我想等到我的 rowSet 被数据填满。
请帮忙 在此先感谢...!
import io.vertx.pgclient.RgPool;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
class TestAsyncOperation{
private RowSet<Row> rowSet;
public RowSet<Row> getResult(){
PgPool conn = DbConfig.getConnection();// consider db connections is received
String qry = "select col1 as column1, col2 as column2 from SomeTable";
conn.query(qry).execute(asyncResult -> {
if(asyncResult.succeeded()){
rowSet = asyncResult.result();
}else{
throw new Exception(asyncResult.cause().getMessage());
}
Thread.sleep(1000);
if(Optional.ofNullable(rowSet).isPresent){
return rowSet;
}else{
return null;
}
});
}
}
【问题讨论】:
-
你所做的不是异步的,所以没有必要使用异步操作。
-
什么是io.vertx.pgclient.RgPool?在 maven Central 中找不到它。
-
if(Optional.ofNullable(rowSet).isPresent) { return rowSet; }else{ return null; }是(让缺少的()放在一边)实现与return rowSet;完全相同的混淆方式
标签: java asynchronous concurrency vert.x