STS allows to bypass any checked exceptions
Ron Pressler
ron.pressler at oracle.com
Sun May 14 22:42:16 UTC 2023
Both are awful and the second isn’t only awful but clearly uses the API in a way the documentation warns against. The purpose of STS is for the scope to not handle subtasks individually but always as a unit. Therefore, exception and result handling should be done collectively. However, we offer a way to get individual subtasks’ results to address the specific but common case where different subtasks return different types. The ISE thrown by get means one thing: there’s a bug in your code because you’ve used the API incorrectly.
If you find your scope code interested in a particular task’s exception, you’re either doing something advanced or wrong. In fact, if you’re using STS without a policy you’re either wrong or advanced.
One decision we wanted to postpone was whether or not to force users to use a policy. If real users end up doing things similar to what you’re demonstrating — which I guess we'll find out during the preview — that may well be what we do. We wanted to keep the option of not using a policy open for the benefit of advanced users, and obviously they can use the API incorrectly (in this case — intentionally). But if regular users fall into the same trap, we may choose to enforce what is now merely a recommendation.
— Ron
> On 13 May 2023, at 17:33, Remi Forax <forax at univ-mlv.fr> wrote:
>
> The more I think about the way STS/TaskHandle manages checked exceptions the less I like it.
>
> Here is my problem:
>
> Do you agree that this code is kind of awful ?
>
> static <T> T bypassCheckedException(Callable<? extends T> callable) {
> try {
> return callable.call();
> } catch (Exception e) {
> throw new IllegalStateException("task complete with exception");
> }
> }
>
> so why this other one is not
>
> static <T> T bypassCheckedException(Callable<? extends T> callable) {
> try(var sts = new StructuredTaskScope<>()) {
> var task = sts.fork(callable);
> try {
> sts.join();
> } catch (InterruptedException e) {
> Thread.currentThread().interrupt();
> }
> return task.get();
> }
> }
>
> given that it has exactly the same semantics ?
>
> regards,
> Rémi
More information about the loom-dev
mailing list