Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Java19/Examples"
Line 239: | Line 239: | ||
<source lang="java"> | <source lang="java"> | ||
package pkg1; | package pkg1; | ||
− | |||
public class B { | public class B { | ||
static Thread currentThread; | static Thread currentThread; | ||
− | |||
@SuppressWarnings("preview") | @SuppressWarnings("preview") | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Runnable runnable = () -> { | Runnable runnable = () -> { | ||
System.out.println("---Start-Run---"); | System.out.println("---Start-Run---"); | ||
− | |||
if (currentThread.isVirtual()) { | if (currentThread.isVirtual()) { | ||
System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview"); | System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview"); | ||
Line 256: | Line 253: | ||
System.out.println("---End---Run---"); | System.out.println("---End---Run---"); | ||
}; | }; | ||
− | // Normal thread is non-daemon by default | + | currentThread = new Thread(runnable); // Normal thread is non-daemon by default |
− | + | ||
currentThread.start(); | currentThread.start(); | ||
− | + | currentThread = Thread.startVirtualThread(runnable); // Virtual thread is by default daemon | |
− | + | try { | |
− | currentThread = Thread.startVirtualThread(runnable); | + | Thread.sleep(100); // Sleep for 100 ms |
− | try | + | |
− | + | ||
− | Thread.sleep(100); | + | |
} catch (InterruptedException e) { | } catch (InterruptedException e) { | ||
e.printStackTrace(); | e.printStackTrace(); | ||
Line 274: | Line 267: | ||
<br> | <br> | ||
[[File:Positive_Test_case1_Virtual_thread.png]] | [[File:Positive_Test_case1_Virtual_thread.png]] | ||
+ | |Code compiles and prints below message:<br> | ||
+ | ---Start-Run---<br> | ||
+ | Standard Platform Thread:<br> | ||
+ | isVirtual: false<br> | ||
+ | isDaemon : false<br> | ||
+ | ---End---Run---<br> | ||
+ | ---Start-Run---<br> | ||
+ | Virtual Thread:<br> | ||
+ | Java19 feature running with VM arguments:<br> | ||
+ | --enable-preview<br> | ||
+ | isVirtual: true<br> | ||
+ | isDaemon : true<br> | ||
+ | ---End---Run---<br> | ||
+ | |- | ||
+ | |- | ||
+ | ! scope="row" | Positive compilation2 (Virtual Threads Example) | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | package pkg1; | ||
+ | public class C { | ||
+ | static Thread currentThread; | ||
+ | @SuppressWarnings("preview") | ||
+ | public static void main(String[] args) { | ||
+ | Runnable runnable = () -> { | ||
+ | System.out.println("---Start-Run---"); | ||
+ | if (currentThread.isVirtual()) { | ||
+ | System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview"); | ||
+ | } else { | ||
+ | System.out.println("Standard Platform Thread:"); | ||
+ | } | ||
+ | System.out.println("isVirtual: " + currentThread.isDaemon() + "\nisDaemon : " + currentThread.isDaemon()); | ||
+ | System.out.println("---End---Run---"); | ||
+ | }; | ||
+ | currentThread = new Thread(runnable); // Normal thread is non-daemon by default | ||
+ | currentThread.start(); | ||
+ | Thread.Builder threadBuilder = Thread.ofVirtual().name("Virtual-Thread", 1); | ||
+ | currentThread = threadBuilder.start(runnable); // Virtual thread is by default daemon | ||
+ | try { | ||
+ | Thread.sleep(100); // Sleep for 100 ms | ||
+ | } catch (InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | }</source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Positive_Test_case2_Virtual_thread.png]] | ||
|Code compiles and prints below message:<br> | |Code compiles and prints below message:<br> | ||
---Start-Run---<br> | ---Start-Run---<br> |
Revision as of 05:36, 20 September 2022
This is an informal page listing examples of features that are implemented by the Java 19 Support, which can be installed from the Marketplace. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here
Watch out for additional examples being added soon.
NOTE:
- Pattern Matching for switch is a preview feature in Java 19. They are not enabled by default and can by enabled using --enable-preview.
- Record Patterns is a preview feature in Java 19. They are not enabled by default and can by enabled using --enable-preview.
- Virtual Threads is a preview feature in Java 19. They are not enabled by default and can by enabled using --enable-preview.
- Structured Concurrency is a incubator features in Java 19.
- In Eclipse, --enable-preview can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace.
Feature / Steps | Expected Result | |
---|---|---|
Standard Java/JRE setup with Eclipse | More details can be found on dedicated Java/JRE setup page. | Setting up Java/JRE in Eclipse. |
Overview of eclipse.ini | More details can be found on dedicated eclipse.ini page. | Specifying the JVM in eclipse.ini |
Preview Feature: Pattern Matching for switch | ||
Postive compilation1 (Pattern Matching for switch Example) | Use the following code:
public class X { private static void foo(Object o) { switch (o) { case Integer i -> System.out.println("String:"); case String s -> System.out.println("String: Hello World!"); default -> System.out.println("Object"); } } public static void main(String[] args) { foo("Hello World"); } } |
Code compiles and prints below message: String: Hello World! |
Positive compilation2 (Pattern Matching for switch Example) | Use the following code:
public class X { private static void foo(Object o) { switch (o) { case Integer I: System.out.println("Integer"); break; case String s && s.length()>1: System.out.println("String > 1"); break; case String s1: System.out.println("String"); break; case X x: System.out.println("X"); break; default : System.out.println("Object"); } } public static void main(String[] args) { foo("Hello World!"); foo("H"); foo(bar()); } public static Object bar() { return new Object(); } } |
Code compiles and prints below message: String > 1 |
Positive compilation3 (Pattern Matching for switch Example) | Use the following code:
public class X { private static void foo(Object o) { switch (o) { case Integer I: System.out.println("Integer"); System.out.println(I); break; case String s && s.length()>1: System.out.println("String s && s.length()>1"); System.out.println(s); break; case String s: System.out.println("String"); System.out.println(s); break; case X x: System.out.println("X"); System.out.println(x); break; default : System.out.println("Object"); } } public static void main(String[] args) { foo("Hello World!"); foo("H"); foo(bar()); } public static Object bar() { return new Object(); } } |
Code compiles and prints below message: String s && s.length()>1 |
Positive compilation4 (Pattern Matching for switch Example) | Use the following code:
public class X { public static void main(String[] args) { foo(Integer.valueOf(11)); foo(Integer.valueOf(9)); } private static void foo(Object o) { switch (o) { case Integer i && i>10: System.out.println("Greater than 10:" + o); break; case Integer j && j>0: System.out.println("Greater than 0:" + o); break; default: System.out.println("Object" + o); } } } |
Code compiles and prints below message: Greater than 10:11 Greater than 0:9 |
Positive compilation5 (Pattern Matching for switch Example) | Use the following code:
public class X { public static void foo(Object o) throws Exception { switch (o) { case String s1: throw new Exception(s1); default: break; } } public static void main(String[] args) throws Exception { foo("hello"); } } |
Code compiles and prints below exception stack trace message: Exception in thread "main" java.lang.Exception: hello |
Preview Feature: Record Patterns | ||
Positive compilation1 (Record Patterns Example) | Use the following code:
package pkg1; public class A { private static void printNumber(Object obj) { if (obj instanceof Float f) { System.out.println("Float value: " + f); } else if (obj instanceof Double d) { System.out.println("Double value: " + d); } else if (obj instanceof Long l) { System.out.println("Long value: " + l); } else if (obj instanceof Integer i) { System.out.println("Integer value: " + i); } else if (obj instanceof Short s) { System.out.println("Short value: " + s); } else if (obj instanceof Byte b) { System.out.println("Byte value: " + b); } else { System.out.println("Unknown Number type" + obj); } } public static void main(String[] args) { printNumber((byte) 1); printNumber((short) 2); printNumber(3); printNumber((long) 4); printNumber(5.0f); printNumber(6.0); } } |
Code compiles and prints below message: Byte value: 1 |
Preview Feature: Virtual Threads | ||
Positive compilation1 (Virtual Threads Example) | Use the following code:
package pkg1; public class B { static Thread currentThread; @SuppressWarnings("preview") public static void main(String[] args) { Runnable runnable = () -> { System.out.println("---Start-Run---"); if (currentThread.isVirtual()) { System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview"); } else { System.out.println("Standard Platform Thread:"); } System.out.println("isVirtual: " + currentThread.isDaemon() + "\nisDaemon : " + currentThread.isDaemon()); System.out.println("---End---Run---"); }; currentThread = new Thread(runnable); // Normal thread is non-daemon by default currentThread.start(); currentThread = Thread.startVirtualThread(runnable); // Virtual thread is by default daemon try { Thread.sleep(100); // Sleep for 100 ms } catch (InterruptedException e) { e.printStackTrace(); } } } |
Code compiles and prints below message: ---Start-Run--- |
Positive compilation2 (Virtual Threads Example) | Use the following code:
package pkg1; public class C { static Thread currentThread; @SuppressWarnings("preview") public static void main(String[] args) { Runnable runnable = () -> { System.out.println("---Start-Run---"); if (currentThread.isVirtual()) { System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview"); } else { System.out.println("Standard Platform Thread:"); } System.out.println("isVirtual: " + currentThread.isDaemon() + "\nisDaemon : " + currentThread.isDaemon()); System.out.println("---End---Run---"); }; currentThread = new Thread(runnable); // Normal thread is non-daemon by default currentThread.start(); Thread.Builder threadBuilder = Thread.ofVirtual().name("Virtual-Thread", 1); currentThread = threadBuilder.start(runnable); // Virtual thread is by default daemon try { Thread.sleep(100); // Sleep for 100 ms } catch (InterruptedException e) { e.printStackTrace(); } } } |
Code compiles and prints below message: ---Start-Run--- |