-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopedValues.java
More file actions
107 lines (94 loc) · 4.58 KB
/
Copy pathScopedValues.java
File metadata and controls
107 lines (94 loc) · 4.58 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package java20.scopedvalues;
/**
* Java 20 Scoped Values (Incubator)
* Demonstrates immutable thread-local data sharing
*
* REQUIREMENTS: Java 20 or later
*
* Package History:
* - Java 20: jdk.incubator.concurrent.ScopedValue (incubator API)
* Compile: javac --add-modules jdk.incubator.concurrent ScopedValues.java
* Run: java --add-modules jdk.incubator.concurrent ScopedValues
*
* - Java 21+: java.util.concurrent.ScopedValue (finalized, standard API)
* No special flags needed
*
* If you see import errors:
* - Your Java version may be too old (requires Java 20+)
* - For Java 20: Ensure incubator module is accessible
* - For Java 21+: Use java.util.concurrent.ScopedValue (current import)
*/
// IMPORT NOTE:
// - Java 20: ScopedValue was in jdk.incubator.concurrent (incubator API)
// Requires: --add-modules jdk.incubator.concurrent
// - Java 21+: ScopedValue is in java.util.concurrent (finalized, standard API)
//
// Using finalized API for IDE compatibility. For Java 20, change to:
// import jdk.incubator.concurrent.ScopedValue;
// and compile with: javac --add-modules jdk.incubator.concurrent ScopedValues.java
/**
* Java 20 Scoped Values Example Code
*
* NOTE: This is a conceptual example. The actual implementation requires:
* - Java 20 or later
* - jdk.incubator.concurrent module access
* - Compilation with: javac --add-modules jdk.incubator.concurrent ScopedValues.java
* - Execution with: java --add-modules jdk.incubator.concurrent ScopedValues
*
* For Java 21+, use: java.util.concurrent.ScopedValue (no incubator module needed)
*/
public class ScopedValues {
public static void main(String[] args) {
System.out.println("=== Java 20 Scoped Values (Incubator) ===\n");
System.out.println("Example code structure:");
System.out.println("""
// For Java 20, use jdk.incubator.concurrent.ScopedValue
import jdk.incubator.concurrent.ScopedValue;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class ScopedValues {
// Define scoped value
private static final ScopedValue<String> USER = ScopedValue.newInstance();
private static final ScopedValue<Integer> REQUEST_ID = ScopedValue.newInstance();
public static void main(String[] args) {
// 1. Basic usage
ScopedValue.runWhere(USER, "Alice", () -> {
String user = USER.get();
System.out.println("User: " + user);
// 2. Nested scope
ScopedValue.runWhere(USER, "Bob", () -> {
System.out.println("User: " + USER.get()); // Bob
});
System.out.println("User: " + USER.get()); // Alice (back to outer scope)
});
// 3. Multiple scoped values
ScopedValue.runWhere(
USER, "Charlie",
REQUEST_ID, 12345,
() -> {
System.out.println("User: " + USER.get() + ", Request ID: " + REQUEST_ID.get());
}
);
// 4. With virtual threads
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
ScopedValue.runWhere(USER, "David", () -> {
executor.submit(() -> {
System.out.println("User in virtual thread: " + USER.get()); // Inherited
});
});
}
}
}
""");
System.out.println("\nKey Features:");
System.out.println("- Immutable thread-local data");
System.out.println("- Inherited by child threads (virtual threads)");
System.out.println("- More efficient than ThreadLocal");
System.out.println("- Structured scoping");
System.out.println("- No memory leaks");
System.out.println("\nCompilation:");
System.out.println(" javac --add-modules jdk.incubator.concurrent ScopedValues.java");
System.out.println(" java --add-modules jdk.incubator.concurrent ScopedValues");
System.out.println("\nNote: For Java 21+, ScopedValue is in java.util.concurrent (no incubator module needed)");
}
}