Future.resultNow / exceptionNow
Alex Otenko
oleksandr.otenko at gmail.com
Sat Nov 20 20:05:32 UTC 2021
These are analogies. Now, I have executor.join, or made sure isDone is
true, but still future.resultNow will throw. I don't know what a good
analogy is. Maybe throwing some runtime exception after checking array
index or reference is non-null.
A good example with Optional is not get() without checks, but x.orElse(y)
without checks.
Maybe you could cure it with more documentation but it feels a bit like
Queue.poll or Map.get throwing because data is not available.
Alex
On Sat, 20 Nov 2021, 17:41 Brian Goetz, <brian.goetz at oracle.com> wrote:
> That would kind of defeat the purpose. If you follow the discipline of
> structured concurrency, where the parent always waits for the child to
> complete (StructuredExecutor offers methods and patterns for guaranteeing
> this), then you will know with confidence that the future is complete, and
> it is safe to just ask for the result. Throwing ISE means you made a
> coding error (i.e., you forgot to wait for the children), like
> dereferencing an object reference without knowing it was non-null, indexing
> into an array without knowing the bounds, or calling Optional::get without
> knowing the Optional holds a value.
>
> Catching ISE here would be like blindly wrapping every dereference with
> "catch NPE", or every array access with "catch AIOOBE".
>
>
>
> On 11/20/2021 11:43 AM, Alex Otenko wrote:
>
> Is there a strong opinion about Future.resultNow and exceptionNow throwing
> IllegalStateException? It seems there will likely be boilerplate
> try-catching, as there is no safe way to inquire in what way the Future is
> isDone.
>
> Returning Optional seems a nicer alternative.
>
> Alex
>
>
>
More information about the loom-dev
mailing list