0

我如何将字符串类型的迭代集值从调用方法返回到未来的对象。我需要将迭代的值存储在数组中并返回该数组还是任何人都可以帮助我

4

2 回答 2

3

使用 a Callable<List<String>>,将其提交给执行程序,执行程序将返回 a Future,其get()方法将返回 a List<String>

ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<List<String>> task = new Callable<List<String>>() {
    public List<String> call() throws Exception {
        List<String> list = new ArrayList<String>();
        // populate list
        return list;
    }
};
Future<List<String>> future = executorService.submit(task);

// the list from above, returns once execution is complete
List<String> list = future.get(); 
于 2012-11-07T12:40:35.553 回答
0

使用返回Object的submit()方法。ExecutorService Future

于 2012-11-07T12:20:27.893 回答