Skip to content

Commit b3edf1c

Browse files
committed
Java 14 examples
1 parent d5ab3e3 commit b3edf1c

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ output/
1313
hs_err_pid*
1414
.vscode/
1515
.DS_Store
16-
*.data
16+
*.data
17+
jdt.ls-java-project/
18+
*.dmg

java-14/HelpfulNPEMessages.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import static java.util.stream.Collectors.toList;
2+
3+
import java.util.stream.IntStream;
4+
5+
/**
6+
* To run: `java -XX:+ShowCodeDetailsInExceptionMessages HelpfulNPEMessages.java`
7+
*/
8+
public class HelpfulNPEMessages {
9+
public static void main(String[] args) {
10+
simpleObjectAccess();
11+
insideStreams();
12+
}
13+
14+
private static void simpleObjectAccess() {
15+
System.out.println("=== Simple Object Attribute Access ===");
16+
try {
17+
var thing = new Thing(11);
18+
System.out.println("Value " + thing.weight.value);
19+
} catch (NullPointerException ex) {
20+
ex.printStackTrace();
21+
}
22+
}
23+
24+
private static void insideStreams() {
25+
System.out.println("%n=== Attribute Access Inside Streams ===");
26+
try {
27+
IntStream.of(2, 4, 6, 8, 9)
28+
.mapToObj(Thing::new)
29+
.map(thing -> thing.weight.value)
30+
.forEach(v -> System.out.println("Val " + v));
31+
} catch (NullPointerException ex) {
32+
ex.printStackTrace();
33+
}
34+
}
35+
}
36+
37+
class Thing {
38+
Weight weight;
39+
40+
Thing(int weight) {
41+
if (weight % 2 == 0) {
42+
this.weight = new Weight(weight);
43+
}
44+
}
45+
46+
public Weight getWeight() {
47+
return weight;
48+
}
49+
}
50+
51+
class Weight {
52+
Integer value;
53+
54+
Weight(int value) {
55+
if (value % 2 == 0) {
56+
this.value = value;
57+
}
58+
}
59+
60+
public int getValue() {
61+
return value;
62+
}
63+
}

java-14/JFREventStreamTest.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.time.Duration;
2+
import java.util.stream.IntStream;
3+
import java.util.ArrayList;
4+
5+
import jdk.jfr.consumer.RecordingStream;
6+
7+
/***
8+
* The profiles configurations is stored in: `$JAVA_HOME/lib/jrf/`
9+
*/
10+
public class JFREventStreamTest {
11+
public static void main(String[] args) throws InterruptedException {
12+
var task = new FibonacciCalculator(Thread.currentThread());
13+
14+
var thread = new Thread(task, "t-fib");
15+
thread.start();
16+
17+
try (var rs = new RecordingStream()) {
18+
rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1));
19+
rs.enable("jdk.GarbageCollection").withPeriod(Duration.ofSeconds(1));
20+
rs.enable("jdk.GCHeapSummary").withPeriod(Duration.ofSeconds(1));
21+
22+
rs.onEvent("jdk.CPULoad", event -> {
23+
System.out.printf("\tCPU load: %f%% - %f%%%n",
24+
(event.getFloat("jvmUser") * 100),
25+
(event.getFloat("machineTotal") * 100)
26+
);
27+
});
28+
rs.onEvent("jdk.GCHeapSummary", event -> {
29+
System.out.printf("\tGC - Summary - %s %f MB%n",
30+
event.getString("when"),
31+
(event.getFloat("heapUsed") / 1_000_000)
32+
);
33+
34+
});
35+
rs.onEvent("jdk.GarbageCollection", event -> {
36+
System.out.printf("\tGC - Cause %s - Durantion: %.04fms%n", event.getString("cause"), (event.getFloat("duration") / 1_000_000));
37+
});
38+
rs.startAsync();
39+
40+
thread.join();
41+
}
42+
43+
}
44+
}
45+
46+
class FibonacciCalculator implements Runnable {
47+
private Thread thread;
48+
49+
FibonacciCalculator(Thread thread) {
50+
this.thread = thread;
51+
}
52+
53+
public void run() {
54+
System.out.println("Starting calculation");
55+
int number = 40;
56+
while (true) {
57+
System.out.println("Calculating for " + number);
58+
var result = calculate(number);
59+
System.out.println("Result for " + number + " is " + result);
60+
if (!thread.isAlive()) {
61+
System.out.println("Ending calculation");
62+
break;
63+
}
64+
65+
number += 5;
66+
}
67+
}
68+
69+
/**
70+
* Bad implementation to take much time.
71+
*/
72+
public long calculate(int n) {
73+
if (n <= 1)
74+
return 1;
75+
var list = new ArrayList<>(n);
76+
IntStream.range(0, n).forEach(__ -> list.add(new Object()));
77+
return calculate(n - 1) + calculate(n - 2);
78+
}
79+
}

java-14/PackagingTool.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import javax.swing.JOptionPane;
2+
3+
/**
4+
* Steps:
5+
*
6+
* - compile: `javac PackagingTool.java`
7+
* - create JAR: `jar -cfe packaging-tool-example.jar PackagingTool PackagingTool.class`
8+
* - test JAR: `java -jar packaging-tool-example.jar`
9+
* - create the Package: `jpackage --name pack-tool-sample --input . --main-jar packaging-tool-example.jar --main-class PackagingTool`
10+
*
11+
* Package will generate:
12+
* - `dmg` on macOS
13+
* - `msi` on Windows
14+
* - `app` on Linux
15+
*/
16+
public class PackagingTool {
17+
public static void main(String[] args) {
18+
System.out.println("Main =)");
19+
JOptionPane.showMessageDialog(null, "Hello world from self-contained application", null,
20+
JOptionPane.INFORMATION_MESSAGE);
21+
}
22+
}

java-14/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ To run each example use: `java --enable-preview --source 14 <FileName.java>`
1010
* Promotion to standard
1111
* Text blocks improvements
1212
* added new flags
13-
* Patttern matching for `instanceof`
13+
* Patttern matching for `instanceof`
1414
* added as Preview
1515
* Records
1616
* added as Preview
17+
* Helpful NullPointerExpections
18+
* flag to enable: `-XX:+ShowCodeDetailsInExceptionMessages`
19+
* Packaging tool to create self-contained Java applications
20+
* bin: `jpackage`
21+
* JFR Event Stream
22+
* [Here](https://github.com/flight-recorder/health-report) is a great example of a tool built
1723

1824
## JEPs
1925

0 commit comments

Comments
 (0)