-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuredConcurrency.java
More file actions
91 lines (75 loc) · 4.55 KB
/
Copy pathStructuredConcurrency.java
File metadata and controls
91 lines (75 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package java22.structuredconcurrency;
/**
* Java 22 Structured Concurrency (Second Preview)
* Demonstrates treating groups of tasks as a unit
*
* NOTE: Requires --enable-preview flag for compilation and execution
* Compile: javac --enable-preview --release 22 StructuredConcurrency.java
* Run: java --enable-preview StructuredConcurrency
*/
public class StructuredConcurrency {
public static void main(String[] args) {
System.out.println("=== Java 22 Structured Concurrency (Second Preview) ===\n");
System.out.println("NOTE: This is a preview feature requiring --enable-preview flag.\n");
System.out.println("Example code structure:");
System.out.println("""
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.Future;
public class StructuredConcurrency {
public static void main(String[] args) {
// 1. Basic structured concurrency
try (var scope = new StructuredTaskScope.ShutdownOnFailure<>()) {
Future<String> user = scope.fork(() -> fetchUser("123"));
Future<String> order = scope.fork(() -> fetchOrder("456"));
scope.join();
scope.throwIfFailed();
System.out.println("User: " + user.resultNow());
System.out.println("Order: " + order.resultNow());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
// 2. Shutdown on success
try (var scope = new StructuredTaskScope.ShutdownOnSuccess<String>()) {
scope.fork(() -> fetchUser("789"));
scope.fork(() -> fetchUser("999"));
String result = scope.join().result();
System.out.println("First successful result: " + result);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
// 3. Practical example - fetch multiple related data
try (var scope = new StructuredTaskScope.ShutdownOnFailure<>()) {
Future<String> user = scope.fork(() -> fetchUser(userId));
Future<String> profile = scope.fork(() -> fetchProfile(userId));
Future<String> preferences = scope.fork(() -> fetchPreferences(userId));
scope.join();
scope.throwIfFailed();
System.out.println("User: " + user.resultNow());
System.out.println("Profile: " + profile.resultNow());
System.out.println("Preferences: " + preferences.resultNow());
} catch (Exception e) {
System.err.println("Failed to fetch user data: " + e.getMessage());
}
}
}
""");
System.out.println("\nKey Features:");
System.out.println("- Structured lifecycle: Tasks are managed as a unit");
System.out.println("- Error propagation: Failures are properly handled");
System.out.println("- Shutdown strategies: ShutdownOnFailure and ShutdownOnSuccess");
System.out.println("- Automatic cleanup: Resources are cleaned up automatically");
System.out.println("- Prevents thread leaks: All tasks complete before scope closes");
System.out.println("\nShutdown Strategies:");
System.out.println("- ShutdownOnFailure: Shuts down if any task fails");
System.out.println("- ShutdownOnSuccess: Shuts down when first task succeeds");
System.out.println("\nBenefits:");
System.out.println("- Better error handling");
System.out.println("- Prevents thread leaks");
System.out.println("- Clearer code structure");
System.out.println("- Easier debugging");
System.out.println("- Automatic resource management");
System.out.println("\nNote: This is a preview feature in Java 22.");
System.out.println("Requires --enable-preview flag for compilation and execution.");
System.out.println("In Java 21+, Structured Concurrency is finalized (no preview flag needed).");
}
}