forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStableValues.java
More file actions
executable file
·29 lines (25 loc) · 794 Bytes
/
StableValues.java
File metadata and controls
executable file
·29 lines (25 loc) · 794 Bytes
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
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
//JAVAC_OPTIONS --enable-preview --release 25
//JAVA_OPTIONS --enable-preview
import java.util.function.*;
import java.util.logging.*;
/// Proof: stable-values
/// Source: content/concurrency/stable-values.yaml
///
/// Note: The snippet calls logger.get() on a StableValue<Logger>. In JDK 25,
/// StableValue.supplier() returns a Supplier<T> whose get() provides the same
/// lazy, thread-safe initialization semantics.
class LoggerHolder {
private final Supplier<Logger> logger =
StableValue.supplier(this::createLogger);
Logger getLogger() {
return logger.get();
}
Logger createLogger() {
return Logger.getLogger(getClass().getName());
}
}
void main() {
new LoggerHolder().getLogger();
}