我在这里是新来的,如果对这个话题不对,对不起?

我的问题:

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

10-11 17:39