Skip to content

Commit b2390d1

Browse files
committed
java 19
1 parent 49e0087 commit b2390d1

9 files changed

Lines changed: 290 additions & 9 deletions

File tree

java-18/src/main/java/com/mkyong/java18/HelloApp.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

java-19/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.mkyong</groupId>
7+
<artifactId>java19</artifactId>
8+
<version>1.0</version>
9+
10+
<name>java-19</name>
11+
<url>https://mkyong.com</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>19</maven.compiler.source>
16+
<maven.compiler.target>19</maven.compiler.target>
17+
<java.version>19</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
</dependencies>
22+
23+
<build>
24+
<finalName>java19</finalName>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
<version>3.8.1</version>
30+
<configuration>
31+
<source>19</source>
32+
<target>19</target>
33+
<compilerArgs>--enable-preview</compilerArgs>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mkyong.java19.jep405;
2+
3+
public class JEP405 {
4+
5+
record Point(int x, int y) {
6+
}
7+
8+
static void printSum(Object o) {
9+
if (o instanceof Point p) {
10+
int x = p.x(); // get x()
11+
int y = p.y(); // get y()
12+
System.out.println(x + y);
13+
}
14+
}
15+
16+
static void printSumNew(Object o) {
17+
if (o instanceof Point(int x,int y)) { // record pattern
18+
System.out.println(x + y);
19+
}
20+
}
21+
22+
public static void main(String[] args) {
23+
printSumNew(new Point(10, 20)); // output 30
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mkyong.java19.jep405;
2+
3+
public class JEP405_1 {
4+
5+
record Point(int x, int y) {
6+
}
7+
8+
record Total(Point p1, Point p2) {
9+
}
10+
11+
static void printSum(Object o) {
12+
// record nested pattern
13+
if (o instanceof Total(Point(int x,int y),Point(int x2,int y2))) {
14+
System.out.println(x + y + x2 + y2);
15+
}
16+
}
17+
18+
public static void main(String[] args) {
19+
20+
printSum(new Total(new Point(10, 5), new Point(2, 3)));
21+
22+
}
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.mkyong.java19.jep424;
2+
3+
import java.lang.foreign.FunctionDescriptor;
4+
import java.lang.foreign.Linker;
5+
import java.lang.foreign.MemoryAddress;
6+
import java.lang.foreign.MemorySegment;
7+
import java.lang.foreign.SegmentAllocator;
8+
import java.lang.foreign.SymbolLookup;
9+
import java.lang.foreign.ValueLayout;
10+
import java.lang.invoke.MethodHandle;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
14+
public class JEP424_SORT {
15+
16+
public static void main(String[] args) throws Throwable {
17+
18+
// 1. Find foreign function on the C library path
19+
Linker linker = Linker.nativeLinker();
20+
SymbolLookup stdlib = linker.defaultLookup();
21+
22+
// 2. Allocate on-heap memory to store strings
23+
String[] javaStrings = {"d", "z", "b", "c", "a"};
24+
25+
// 3. Allocate off-heap memory to store pointers
26+
SegmentAllocator allocator = SegmentAllocator.implicitAllocator();
27+
MemorySegment offHeap = allocator.allocateArray(ValueLayout.ADDRESS, javaStrings.length);
28+
29+
// 4. Copy the strings from on-heap to off-heap
30+
for (int i = 0; i < javaStrings.length; i++) {
31+
32+
// Allocate a string off-heap, then store a pointer to it
33+
MemorySegment cString = allocator.allocateUtf8String(javaStrings[i]);
34+
offHeap.setAtIndex(ValueLayout.ADDRESS, i, cString);
35+
}
36+
37+
MethodHandle radixSort = linker.downcallHandle(
38+
stdlib.lookup("radixsort").orElseThrow(),
39+
FunctionDescriptor.ofVoid(ADDRESS, JAVA_INT, ADDRESS, JAVA_CHAR));
40+
41+
// 5. Sort the off-heap data by calling the foreign function
42+
radixSort.invoke(offHeap, javaStrings.length, MemoryAddress.NULL, '\0');
43+
44+
// 6. Copy the (reordered) strings from off-heap to on-heap
45+
for (int i = 0; i < javaStrings.length; i++) {
46+
MemoryAddress cStringPtr = offHeap.getAtIndex(ValueLayout.ADDRESS, i);
47+
javaStrings[i] = cStringPtr.getUtf8String(0);
48+
}
49+
50+
//print sort result
51+
for (String javaString : javaStrings) {
52+
System.out.println(javaString);
53+
}
54+
55+
}
56+
57+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.mkyong.java19.jep424;
2+
3+
import java.lang.foreign.*;
4+
import java.lang.invoke.MethodHandle;
5+
6+
import static java.lang.foreign.ValueLayout.ADDRESS;
7+
import static java.lang.foreign.ValueLayout.JAVA_LONG;
8+
9+
public class JEP424_STRLEN {
10+
11+
public static void main(String[] args) throws Throwable {
12+
13+
String input = "Hello World";
14+
15+
// 1. Find foreign function on the C library path
16+
SymbolLookup stdlib = Linker.nativeLinker().defaultLookup();
17+
18+
// 2. Get a handle to the "strlen" function in the C standard library
19+
MethodHandle methodHandle = Linker.nativeLinker().downcallHandle(
20+
stdlib.lookup("strlen").orElseThrow(),
21+
FunctionDescriptor.of(JAVA_LONG, ADDRESS));
22+
23+
// 3. Allocate off-heap memory to store strings
24+
MemorySegment memorySegment = SegmentAllocator
25+
.implicitAllocator().allocateUtf8String(input);
26+
27+
// 4. Runs the foreign function "strlen"
28+
long length = (long) methodHandle.invoke(memorySegment);
29+
30+
System.out.println("length = " + length);
31+
32+
}
33+
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mkyong.java19.jep425;
2+
3+
import java.time.Duration;
4+
import java.util.concurrent.Executors;
5+
import java.util.stream.IntStream;
6+
7+
public class JEP425 {
8+
9+
public static void main(String[] args) {
10+
11+
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
12+
IntStream.range(0, 10_000).forEach(i -> {
13+
executor.submit(() -> {
14+
Thread.sleep(Duration.ofSeconds(1));
15+
return i;
16+
});
17+
});
18+
} // executor.close() is called implicitly, and waits
19+
20+
/*try (var executor = Executors.newFixedThreadPool(20)) {
21+
IntStream.range(0, 10_000).forEach(i -> {
22+
executor.submit(() -> {
23+
Thread.sleep(Duration.ofSeconds(1));
24+
return i;
25+
});
26+
});
27+
}*/
28+
29+
System.out.println("Done");
30+
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mkyong.java19.jep427;
2+
3+
public class JEP427 {
4+
5+
public static void main(String[] args) {
6+
7+
testJava19("mkyong");
8+
testJava19("mkyongmkyong");
9+
}
10+
11+
/* Old guarded pattern
12+
static void test(Object o) {
13+
switch (o) {
14+
case String s && s.length() > 6 -> System.out.println("String's length longer than 10!");
15+
case String s -> System.out.println("String's length is " + s.length());
16+
default -> {
17+
}
18+
}
19+
}*/
20+
21+
//Guarded patterns are replaced with when clauses in switch blocks.
22+
static void testJava19(Object o) {
23+
switch (o) {
24+
case String s
25+
when s.length() > 10 -> System.out.println("String's length longer than 10!");
26+
case String s -> System.out.println("String's length is " + s.length());
27+
default -> {}
28+
}
29+
}
30+
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.mkyong.java19.jep428;
2+
3+
import java.time.Duration;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.Future;
7+
8+
public class JEP428 {
9+
10+
public static void main(String[] args) throws ExecutionException, InterruptedException {
11+
JEP428 obj = new JEP428();
12+
obj.handleUnStructureAPI();
13+
}
14+
15+
Response handleUnStructureAPI() throws ExecutionException, InterruptedException {
16+
try (var executor = Executors.newFixedThreadPool(10)) {
17+
Future<String> user = executor.submit(this::findUser);
18+
Future<Integer> order = executor.submit(this::fetchOrder);
19+
String theUser = user.get(); // Join findUser
20+
int theOrder = order.get(); // Join fetchOrder
21+
return new Response(theUser, theOrder);
22+
}
23+
}
24+
25+
/*Response handleStructureAPI() throws ExecutionException, InterruptedException {
26+
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
27+
Future<String> user = scope.fork(this::findUser);
28+
Future<Integer> order = scope.fork(this::fetchOrder);
29+
30+
scope.join(); // Join both forks
31+
scope.throwIfFailed(); // ... and propagate errors
32+
33+
// Here, both forks have succeeded, so compose their results
34+
return new Response(user.resultNow(), order.resultNow());
35+
}
36+
}*/
37+
38+
private String findUser() throws InterruptedException {
39+
Thread.sleep(Duration.ofSeconds(1));
40+
return "mkyong";
41+
}
42+
43+
private Integer fetchOrder() throws InterruptedException {
44+
Thread.sleep(Duration.ofSeconds(1));
45+
return 1;
46+
}
47+
48+
record Response(String x, int y) {
49+
}
50+
}

0 commit comments

Comments
 (0)