1

我正在使用 ExecutorService 运行一些 Callable 线程。线程在提交给 ExecutorService 之前使用数据进行初始化。

在处理 Future.get() 抛出的异常时,我想用原始数据记录一条消息。是否可以从 Future 对象返回到创建它的原始线程?

伪代码:

void run(List<Data> dataList) {
    List<Future<Foo>> results = new ArrayList<Future<Foo>>();
    for (Data data : dataList) {
        Callable<Foo> thread = new FooCallable(data);
        Future<Foo> result = this.executorService.submit(thread);
        results.add(result);
    }

    ...

    for (Future<Foo> result : results) {
        Foo foo;
        try {
            foo = result.get();
        } catch (InterruptedException e) {
            //
            // I would like access to the original Data object here
            //
        } catch (ExecutionException e) {
            //
            // and here
            //
        }
    }
}
4

2 回答 2

0

是否可以从 Future 对象返回到创建它的原始线程?

我认为您的意思是您想访问Data导致异常的字段。您可以通过创建一个小容器类来做到这一点:

private static class FutureData {
   final Data data;
   final Future<Foo> future;
   public FutureData(Data data, Future<Foo> future) {
      this.data = data;
      this.future = future;
   }
}

然后,当您创建未来时,您将添加一个FutureData到您的results列表中。

List<FutureData> results = new ArrayList<FutureData>();
for (Data data : dataList) {
    Callable<Foo> callable = new FooCallable(data);
    Future<Foo> future = this.executorService.submit(callable);
    results.add(new FutureData(data, future));
}

然后你可以这样做:

for (FutureData futureData : results) {
    Foo foo;
    try {
        foo = futureData.future.get();
    } catch (Exception e) {
       // use futureData.data here
       ...
于 2013-05-24T21:09:47.573 回答
0

使用 Map 而不是 List 来存储您的 Futures 及其相应的数据,并使用 Futures 作为该映射的键:

Map<Future<Foo>,Data> results = new HashMap<Future<Foo>,Data>();
for (Data data : dataList) {
    Callable<Foo> thread = new FooCallable(data);
    Future<Foo> result = this.executorService.submit(thread);
    results.put(result,data);
}

Iterator<Map.Entry<Future<Foo>,Data>> resultsIterator = results.entrySet().iterator();
while(resultsIterator.hasNext()) {
    Map.Entry<Future<Foo>,Data> entry = resultsIterator.next();
    Future<Foo> future = entry.getKey();
    Data data = entry.getValue();
    Foo foo;
    try {
        foo = future.get();
    } catch (InterruptedException e) {
        //data accessible here
    } catch (ExecutionException e) {
        //data accessible here too
    }
}
于 2013-05-24T21:28:52.157 回答