Skip to content

Commit 7a9dadc

Browse files
committed
Update structured concurrency example
1 parent 9bf47c5 commit 7a9dadc

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

10-network/lecture/slides.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)