[英]How can I run JDK 19 with Structured Concurrency?
我想嘗試在JEP 428: Structured Concurrency (Incubator)中定義的新Project Loom功能
我的 pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<compilerArgs>--source 19</compilerArgs>
<compilerArgs>--enable-preview</compilerArgs>
<compilerArgs>--add-modules jdk.incubator.concurrent</compilerArgs>
<compilerVersion>19</compilerVersion>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
但是當我嘗試通過mvn compile
構建時,我得到了
[ERROR] /C:/Users/ERIC/Documents/git/loom-lab/laboratory/src/main/java/net/kolotyluk/loom/Structures.java:[3,21] package jdk.incubator.concurrent is not visible
(package jdk.incubator.concurrent is declared in module jdk.incubator.concurrent, which is not in the module graph)
有沒有我想念的秘方?
$ mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T11:41:47-07:00)
Maven home: C:\Program Files (Open)\Apache\apache-maven-3.6.0
Java version: 19-ea, vendor: Oracle Corporation, runtime: C:\Program Files (Open)\jdk-19
Default locale: en_CA, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
我不了解所有活動部件,但我確實成功訪問了 Java 19 中正在孵化和預覽的新 Project Loom 功能。
這是我的main
方法。
record Event( UUID id , Instant when , Integer reading ) {}
try ( var scope = new StructuredTaskScope.ShutdownOnFailure() )
{
Future < UUID > futureId = scope.fork( ( ) -> UUID.randomUUID() );
Future < Instant > futureWhen = scope.fork( ( ) -> Instant.now() );
Future < Integer > futureReading = scope.fork( ( ) -> ThreadLocalRandom.current().nextInt( 1 , 10 ) );
scope.join(); // Join both forks
scope.throwIfFailed(); // ... and propagate errors
Event event = new Event( futureId.get() , futureWhen.get() , futureReading.get() );
System.out.println( event );
}
catch ( InterruptedException e )
{
throw new RuntimeException( e );
}
catch ( ExecutionException e )
{
throw new RuntimeException( e );
}
該代碼使用這些輸入:
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
運行時:
事件[id=de316aca-10b1-41ed-bfc6-732c4e184566, when=2022-08-07T03:04:48.207650Z, reading=9]
我在src/main/java
的 package 層次結構之外添加了一個module-info.java
文件。
還有我的POM
。 我從 Apache Maven 快速入門工件開始。 然后我將所有版本號更新到最新。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil.example</groupId>
<artifactId>Loom</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Loom</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>19</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (maybe moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<!--<compilerVersion>19</compilerVersion>-->
<release>19</release>
<!--<compilerArgs>--source 19</compilerArgs>-->
<compilerArgs>--enable-preview</compilerArgs>
<!--<compilerArgs>--add-modules jdk.incubator.concurrent</compilerArgs>-->
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M3</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@BasilBourque 的一個小補充很好的答案。 對於那些可能不使用 Maven 或模塊的人,您不需要將您的應用程序設為模塊。 所有啟動器都需要--enable-preview --add-modules jdk.incubator.concurrent
的參數,無論是直接啟動還是通過 Maven。
您應該能夠使用以下源代碼啟動器或 javac/java 從命令行使用標准類路徑和沒有 module-info.java 運行他提供的並發示例:
%JAVAHOME%/bin/java --source 19 --enable-preview --add-modules jdk.incubator.concurrent ConcurrencyEx.java
或者編譯然后運行它:
%JAVAHOME%/bin/javac --source 19 --enable-preview --add-modules jdk.incubator.concurrent -d ../build ConcurrencyEx.java
%JAVAHOME%/bin/java --enable-preview --add-modules jdk.incubator.concurrent -cp ../build ConcurrencyEx
顯然,只需將%JAVAHOME%
替換$JAVAHOME
即可在 Linux 而不是 Windows 上運行。
源代碼:
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import jdk.incubator.concurrent.*;
public class ConcurrencyEx {
// main as from @BasilBourque answer
public static void main(String[] args) throws InterruptedException, ExecutionException {
record Event( UUID id , Instant when , Integer reading ) {}
try ( var scope = new StructuredTaskScope.ShutdownOnFailure() ) {
Future < UUID > futureId = scope.fork( ( ) -> UUID.randomUUID() );
Future < Instant > futureWhen = scope.fork( ( ) -> Instant.now() );
Future < Integer > futureReading = scope.fork( ( ) -> ThreadLocalRandom.current().nextInt( 1 , 10 ) );
scope.join(); // Join both forks
scope.throwIfFailed(); // ... and propagate errors
Event event = new Event( futureId.get() , futureWhen.get() , futureReading.get() );
System.out.println( event );
}
}
}
我設法讓它與 Maven 一起工作,而不需要module-info.java
文件。 我的 POM 和你的 POM 之間的主要區別似乎是我如何設置編譯器 arguments。 而不是你所擁有的:
<configuration>
<compilerArgs>--source 19</compilerArgs>
<compilerArgs>--enable-preview</compilerArgs>
<compilerArgs>--add-modules jdk.incubator.concurrent</compilerArgs>
<compilerVersion>19</compilerVersion>
<source>19</source>
<target>19</target>
</configuration>
我有:
<configuration>
<compilerArgs>
<arg>--add-modules=jdk.incubator.concurrent</arg>
</compilerArgs>
</configuration>
注意我有嵌套在<compilerArgs>
中的<arg>
元素。 我在--add-modules
參數中也有=
,但我不知道這是否是絕對必要的。 似乎只有在運行時才需要--enable-preview
,而不是編譯時。 您擁有的其他 arguments 我設置了不同的方式(例如, <properties>
元素)。
這是一個示例,其中JAVA_HOME
設置為 JDK 19 安裝。
App.java:
package sample;
import jdk.incubator.concurrent.StructuredTaskScope;
public class App {
public static void main( String[] args ) throws Exception {
try (var scope = new StructuredTaskScope<Void>()) {
scope.fork(() -> delayPrint(1000, "Hello,"));
scope.fork(() -> delayPrint(2000, "World!"));
scope.join();
}
System.out.println("Done!");
}
private static Void delayPrint(long delay, String message) throws Exception {
Thread.sleep(delay);
System.out.println(message);
return null;
}
}
pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<compilerArgs>
<arg>--add-modules=jdk.incubator.concurrent</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<executable>${java.home}/bin/java</executable>
<arguments>
<argument>--add-modules=jdk.incubator.concurrent</argument>
<argument>--enable-preview</argument>
<argument>--class-path</argument>
<classpath/>
<argument>sample.App</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
編譯:
> mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:sample >-------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\***\Desktop\structured_concurrency\sample\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ sample ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\***\Desktop\structured_concurrency\sample\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.439 s
[INFO] Finished at: 2022-08-08T01:01:26-06:00
[INFO] ------------------------------------------------------------------------
執行:
> mvn exec:exec
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:sample >-------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:exec (default-cli) @ sample ---
WARNING: Using incubator modules: jdk.incubator.concurrent
Hello,
World!
Done!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.610 s
[INFO] Finished at: 2022-08-08T01:02:42-06:00
[INFO] ------------------------------------------------------------------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.