Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Add Task.whenAllResult to collect results of multiple parallel tasks #46

Merged
merged 1 commit into from
Jan 23, 2015
Merged

Add Task.whenAllResult to collect results of multiple parallel tasks #46

merged 1 commit into from
Jan 23, 2015

Conversation

josephearl
Copy link

Creates a task that completes when all of the provided tasks are complete.

If none of the tasks faulted and none of the tasks were cancelled, the resulting task will end completed. The result of the returned task will be set to a list containing all of the results of the supplied tasks in the same order as they were provided (e.g. if the input tasks collection contained t1, t2, t3, the output task's result will return an List<TResult> where code list.get(0) == t1.getResult(), list.get(1) == t2.getResult(), and list.get(2) == t3.getResult()}.

@grantland
Copy link
Member

Re: Task.whenAllTResult

This seems to make a drastic change to whenAll where we will now keep references to all results until all inner tasks are complete. This would be a problem where we want to launch a bunch of large file downloads where this might now hold all the files in memory until all the tasks complete, when we really just want to write to disk and lose the references to the files as they complete.

It would be better to call whenAll internally from whenAllTResult as opposed to the other way around and construct the result List in the continuation like:

public static Task<List<TResult>> whenAllTResult(final List<Task<TResult>> tasks) {
  return whenAll(tasks).onSuccess(
    // Get the results from tasks and return them in one list.
  );
}

This would prevent holding on to any unwanted references.

Re: Task.whenAllResult

I'm not sure we need this as I don't see many people needing to read the results of many tasks with different types and if they do, they can write one-off code like above. Additionally, it's not part of the .NET spec.

We can remove this and rename Task.whenAllTResult to Task.whenAllResult and simplify our APIs.

Re: AggregateException

I'm sorry about the List vs array change. This has been amended in #47 and it would be great to hear your thoughts on it.

@josephearl
Copy link
Author

@grantland thanks for the feedback, will remove Task.whenAllResult and the changes to AggregateException and leave those for issue #47

I'll also make the recommended changes to whenAllTResult

@josephearl
Copy link
Author

@grantland updated

@josephearl josephearl changed the title Add Task.whenAllResult and Task.whenAllTResult to collect results of multiple parallel tasks Add Task.whenAllResult to collect results of multiple parallel tasks Jan 22, 2015
@grantland
Copy link
Member

Looks perfect! Once you squash your commits, I'll accept the PR.

@josephearl
Copy link
Author

Squashed

* <p/>
* If any of the supplied tasks completes in a faulted state, the returned task will also complete
* in a faulted state, where its exception will resolve to that {@link java.lang.Exception} if a
* single task fails or an {@link AggregateException} of all the {@link java.lang.Exception}s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AggregateException will not only have java.lang.Exception, right? It might have java.lang.Error (basically it contains java.lang.Throwable)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I think it's fine as we only catch Exceptions right now. If/when we change it to Throwables, we'll change the documentation as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we ever want to catch raw Error or Throwable? That would result in things like OOM error getting trapped rather than tearing down the process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More or less to match with newer Java APIs as well as other libraries: ReactiveX/RxJava#296

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm both for and against. If it is implemented, a few errors need to be caught specially to make sure they are not swallowed. Also unobserved exception handling should probably be a pre-requisite for merging that change in order to ensure import errors are not forgotten about.

What happens if the users code encounters a StackOverflowError? We attempt to catch it, then most likely (if user code is running on the same thread) we encounter another StackOverflow attempting to run through the continuations. Except this time, the error comes from Task code so it looks like a library issue and the original error information is lost.

IMO the current situation already traps more things than it should, things like IllegalStateException and NullPointerException which we shouldn't - these errors indicate a programming mistake, not an execution failure and so they should not pollute the user error handling flow like they do currently.

However, too many developers have (over-)used RuntimeExceptions to indicate conditions that should not really crash a program because it saved time thinking about error handling so I don't think it's realistic to change that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hallucinogen
Copy link

One comment, otherwise LGTM

grantland added a commit that referenced this pull request Jan 23, 2015
Add Task.whenAllResult to collect results of multiple parallel tasks
@grantland grantland merged commit c119b2d into BoltsFramework:master Jan 23, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants