File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -183,26 +183,27 @@ Executor обект винаги трябва да бъде спрян с мет
183183
184184- ` fork() ` – стартира задача
185185- ` join() ` – изчаква всички
186- - ` throwIfFailed() ` – спира останалите задачи при грешка
187186- гарантирано няма „висящи“ нишки след края на блока
188187
189188---
190189
191190### Прост пример
192191
193192``` java
194- import java.util.concurrent.StructuredTaskScope ;
193+ public String loadUserPage() {
194+ try (var scope = StructuredTaskScope . < String > open()) {
195195
196- String loadUserPage() {
197- try (var scope = new StructuredTaskScope .ShutdownOnFailure ()) {
196+ // Fork concurrent tasks
198197 var userTask = scope. fork(() - > loadUser());
199198 var postsTask = scope. fork(() - > loadPosts());
200-
201- scope. join(); // Throws if any task failed
202-
203- return userTask. result() + " : " + postsTask. result();
204-
205- } catch (Exception e) {
199+
200+ // Wait for all tasks to complete; throws FailedException if any task fails
201+ scope. join();
202+
203+ // Retrieve results
204+ return userTask. get() + " : " + postsTask. get();
205+
206+ } catch (StructuredTaskScope . FailedException | InterruptedException e) {
206207 throw new UserPageLoadException (" Failed to load user page" , e);
207208 }
208209}
You can’t perform that action at this time.
0 commit comments