Java Callable - >启动线程并等待

时间:2017-05-24 11:09:01

标签: java multithreading

我是新来的,很抱歉,如果我把这个说错了话题?

我的问题:

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

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

1 个答案:

答案 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();