-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopedValues.java
More file actions
85 lines (72 loc) · 3.9 KB
/
Copy pathScopedValues.java
File metadata and controls
85 lines (72 loc) · 3.9 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
package java22.scopedvalues;
/**
* Java 22 Scoped Values (Second Preview)
* Demonstrates immutable thread-local data sharing
*
* NOTE: Requires --enable-preview flag for compilation and execution
* Compile: javac --enable-preview --release 22 ScopedValues.java
* Run: java --enable-preview ScopedValues
*
* In Java 22, ScopedValue is in java.util.concurrent (preview API)
* In Java 21+, it's finalized in java.util.concurrent (standard API)
*/
public class ScopedValues {
public static void main(String[] args) {
System.out.println("=== Java 22 Scoped Values (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.ScopedValue;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class ScopedValues {
// Define scoped values
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 in nested scope: " + USER.get()); // Bob
});
System.out.println("User after nested scope: " + USER.get()); // Alice
});
// 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: Cannot be modified after binding");
System.out.println("- Inherited: Automatically inherited by child threads (including virtual threads)");
System.out.println("- No memory leaks: Automatically cleaned up when scope ends");
System.out.println("- Structured: Scoped to specific code blocks");
System.out.println("- Better performance: More efficient than ThreadLocal");
System.out.println("\nBenefits over ThreadLocal:");
System.out.println("- Safer: Immutable by design");
System.out.println("- No memory leaks: Automatic cleanup");
System.out.println("- Inherited by child threads");
System.out.println("- Better performance");
System.out.println("- Structured scoping");
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+, ScopedValue is finalized (no preview flag needed).");
}
}