Skip to content

Commit 1484609

Browse files
committed
JDK 21: updated readme; added note to indicate which example I've used JDK built from main branch
1 parent 5b12679 commit 1484609

6 files changed

+102
-7
lines changed

Diff for: java-21/InstanceMainMethodLauncher.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/**
2+
* Using JDK manually built from main branch.
3+
*
24
* To run: `java --enable-preview --source 21 InstanceMainMethodLauncher.java`
35
*/
46
class InstanceMainMethodLauncher {

Diff for: java-21/README.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
44

55
## JEPs
66

7-
* [404](https://openjdk.java.net/jeps/404) - Generational Shenandoah (Experimental)
8-
* [430](https://openjdk.java.net/jeps/430) - String Templates (Preview)
9-
* [431](https://openjdk.java.net/jeps/431) - Sequenced Collections
10-
* [439](https://openjdk.java.net/jeps/439) - Generational ZGC
11-
* [440](https://openjdk.java.net/jeps/440) - Record Patterns
12-
* [441](https://openjdk.java.net/jeps/441) - Pattern Matching for switch
7+
* [404](https://openjdk.org/jeps/404) - Generational Shenandoah (Experimental; Target to drop)
8+
* [430](https://openjdk.org/jeps/430) - String Templates (Preview)
9+
* [431](https://openjdk.org/jeps/431) - Sequenced Collections
10+
* [439](https://openjdk.org/jeps/439) - Generational ZGC
11+
* [440](https://openjdk.org/jeps/440) - Record Patterns
12+
* [441](https://openjdk.org/jeps/441) - Pattern Matching for switch
1313
* [442](https://openjdk.org/jeps/442) - Foreign Function & Memory API (Third Preview)
1414
* [443](https://openjdk.org/jeps/443) - Unnamed Patterns and Variables (Preview)
1515
* [444](https://openjdk.org/jeps/444) - Virtual Threads
1616
* [445](https://openjdk.org/jeps/445) - Unnamed Classes and Instance Main Methods (Preview)
17+
* [446](https://openjdk.org/jeps/446) - Scoped Values (Preview)
1718
* [448](https://openjdk.org/jeps/448) - Vector API (Sixth Incubator)
1819
* [449](https://openjdk.org/jeps/449) - Deprecate the Windows 32-bit x86 Port for Removal
1920
* [451](https://openjdk.org/jeps/451) - Prepare to Disallow the Dynamic Loading of Agents
2021
* [452](https://openjdk.org/jeps/452) - Key Encapsulation Mechanism API
22+
* [453](https://openjdk.org/jeps/453) - Structured Concurrency (Preview)
2123

2224
## Features
2325

@@ -26,6 +28,13 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
2628
* changed to make virtual threads always support thread-local
2729
* in preview releases was possible to create a virtual thread without thread-local support
2830
* flag `jdk.traceVirtualThreadLocals` to show the strack trace when a virtual threads sets a value in thread-local variable
31+
* Scoped values
32+
* promoted from incubated to preview feature
33+
* moved from pacote `jdk.incubator.concurrent` to `java.util.concurrent`
34+
* Structured concurrency
35+
* promoted from incubated to preview feature
36+
* moved from pacote `jdk.incubator.concurrent` to `java.util.concurrent`
37+
* changed method `StructuredTaskScope.fork` to return a `Subtask` instanceof of a `Future`
2938
* Record patterns
3039
* promotion to standard
3140
* the main change is remove the support for record pattern in header of an enhanced for loop
@@ -124,6 +133,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
124133
* unnamed class:
125134
* any method declared in a source file without an enclosed class will be considered to be member of an unnamed top-level class
126135
* the compiler requires an unnamed method to have a valid main method to be launched
136+
* we cannot use javadoc tool to generate documation from a unnamed class (doesn't have a accessible API from other class)
127137
* unnamed class is always final and cannot implement or extends any class other than `Object`
128138
* is equivalent to the following usage of anonymous class declaration:
129139
```java

Diff for: java-21/ScopedValueUsageExample.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.concurrent.*;
2+
3+
/**
4+
* Using JDK manually built from main branch.
5+
*
6+
* The only change from JDK 20 is the package to import
7+
* (we no longer need to use add-module jdk.incubator.concurrent).
8+
*
9+
* **Not released in the build yet**
10+
* Run: `java --source 21 --enable-preview ScopedValueUsageExample.java`
11+
*/
12+
public class ScopedValueUsageExample {
13+
final static ScopedValue<String> MAIN_SCOPE = ScopedValue.newInstance();
14+
15+
public static void main(String[] args) {
16+
System.out.println("Starting scoped value");
17+
18+
// we can share a value from here
19+
ScopedValue.where(MAIN_SCOPE, "message from main")
20+
.run(() -> {
21+
var worker = new Worker();
22+
worker.execute();
23+
});
24+
System.out.println("main ending");
25+
}
26+
}
27+
28+
class Worker {
29+
final static ScopedValue<String> WORKER_SCOPE = ScopedValue.newInstance();
30+
31+
public void execute() {
32+
// accessing the value from the scope
33+
System.out.println("shared value from main: " + ScopedValueUsageExample.MAIN_SCOPE.get());
34+
35+
// === Nested Scope ===
36+
// we can create a nested scope
37+
ScopedValue.where(WORKER_SCOPE, "message from worker")
38+
.run(() -> {
39+
// the outmost scope will still works
40+
var messageFromMain = ScopedValueUsageExample.MAIN_SCOPE.get();
41+
var messageFromWorker = WORKER_SCOPE.get();
42+
System.out.println("shared value to inner scope from main: " + messageFromMain);
43+
System.out.println("shared value to inner scope from worker: " + messageFromWorker);
44+
});
45+
46+
// we cannot access it from outside its scope (NoSuchElementException)
47+
// var invalidAccess = WORKER_SCOPE.get();
48+
49+
// === Rebinded Scope Value ===
50+
// we can create a new scope that rebinds a new value to the created scope (when using the same variable)
51+
ScopedValue.where(ScopedValueUsageExample.MAIN_SCOPE, "message from worker over main")
52+
.run(() -> {
53+
// the outmost scope will still works
54+
var rebindedMessage = ScopedValueUsageExample.MAIN_SCOPE.get();
55+
System.out.println("shared value from shadowed scope: " + rebindedMessage);
56+
});
57+
58+
// but the original scope from main will still have its initial value (immutable)
59+
System.out.println("shared value from main after all scopes: " + ScopedValueUsageExample.MAIN_SCOPE.get());
60+
}
61+
}

Diff for: java-21/UnnamedClassMainMethodLauncher.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
/**
2+
* Using JDK manually built from main branch.
3+
*
24
* To run: `java --enable-preview --source 21 UnnamedClassMainMethodLauncher.java`
35
*/
46
void main() {
57
System.out.println("Hello world from a unnamed class with a main method o/");
68
}
9+
10+
/**
11+
To see the generated class:
12+
- `javac --enable-preview --source 21 UnnamedClassMainMethodLauncher.java`
13+
- `javap UnnamedClassMainMethodLauncher`
14+
15+
Output:
16+
17+
```
18+
Compiled from "UnnamedClassMainMethodLauncher.java"
19+
final class UnnamedClassMainMethodLauncher {
20+
UnnamedClassMainMethodLauncher();
21+
void main();
22+
}
23+
```
24+
*/

Diff for: java-21/UnnamedClassWithAllowedMembers.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/**
2+
* Using JDK manually built from main branch.
3+
*
24
* To run: `java --enable-preview --source 21 UnnamedClassWithAllowedMembers.java`
35
*/
46

@@ -20,7 +22,6 @@ static void instanceMethod() {
2022
System.out.println("can have any instance method");
2123
}
2224

23-
2425
void main() {
2526
System.out.println("must always have a valid main method to launch");
2627

Diff for: java-21/UnnamedClassWithInitializers.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/**
2+
* Using JDK manually built from main branch.
3+
* This code is not working with the JDK main branch.
4+
*
25
* To run: `java --enable-preview --source 21 UnnamedClassWithInitializers.java`
36
*/
47

0 commit comments

Comments
 (0)