我是新来的,很抱歉,如果我把这个说错了话题?
我的问题:
class TaskEol implements Callable<ArrayList<Coordinates>> {
ArrayList<CoordinatesEolEwp> coordinates = new ArrayList<Coordinates>();
public ArrayList<Coordinates> call() throws Exception {
new Thread (() -> {
indicatorDatabaseAction.setVisible(true);
coordinates = loadCoordinatesOutOfDatabase();
indicatorDatabaseAction.setVisible(false);
}).start();
return coordinates;
}
}
如何在返回之前等待数据库在对象中写入坐标?
答案 0 :(得分:2)
沿着这些方向,myCallable
是你的可赎回但没有Thread
的东西,即
public ArrayList<Coordinates> call() throws Exception {
indicatorDatabaseAction.setVisible(true);
coordinates = loadCoordinatesOutOfDatabase();
indicatorDatabaseAction.setVisible(false);
return coordinates;
}
...然后
ExecutorService e = Executors.newSingleThreadExecutor();
Future<ArrayList<Coordinates>> f = e.submit(myCallable);
// Note that Future.get() blocks until results are ready
ArrayList<Coordinates> coords = f.get();