Я использую ExecutorService в Java для вызова потоков с invokeAll()
.После этого я получаю набор результатов с future.get()
.Очень важно, чтобы я получал результаты в том же порядке, в котором я создал потоки.
Вот фрагмент:
try {
final List threads = new ArrayList();
// create threads
for (String name : collection)
{
final CallObject object = new CallObject(name);
threads.add(object);
}
// start all Threads
results = pool.invokeAll(threads, 3, TimeUnit.SECONDS);
for (Future<String> future : results)
{
try
{
// this method blocks until it receives the result, unless there is a
// timeout set.
final String rs = future.get();
if (future.isDone())
{
// if future.isDone() = true, a timeout did not occur.
// do something
}
else
{
// timeout
// log it and do something
break;
}
}
catch (Exception e)
{
}
}
}
catch (InterruptedException ex)
{
}
Уверен ли я, что получу результаты из future.get () в том же порядке я создал новые CallObjects и добавил их в мой ArrayList?Я знаю, в документации сказано следующее: invokeAll(): returns a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.
Но я хотел убедиться, что правильно понял ...
Спасибо за ответы!: -)