gpt4 book ai didi

Java Callable -> 启动线程并等待

转载 作者:行者123 更新时间:2023-11-30 02:31:42 29 4
gpt4 key购买 nike

我是新来的,如果我把这个放到错误的主题中,很抱歉?

我的问题:

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;
}
}

如何等待数据库在返回对象之前将坐标写入对象中?

最佳答案

沿着这些线,其中 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();

关于Java Callable -> 启动线程并等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44156630/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com