我已经编辑了这件事,提出了最新的wrt的原始张贴。
我想尝试一下在:https://wiki.openjdk.org/display/loom/Main中定义的新的JEP 428:结构化并发(Incubator)特性
我在我的pom.xml
<properties>
<maven.compiler.executable>${env.JAVA_HOME}/bin/javac</maven.compiler.executable>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
</properties>
. . .
<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>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>其中JAVA_HOME指向JDK 19,但当我试图通过mvn compile构建时,
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Structured.java:3:20: error: package jdk.incubator.concurrent is not visible
[ERROR] C:\Users\ERIC\Documents\git\loom-lab\laboratory\src\main\java\net\kolotyluk\loom\Structures.java:3:20: error: package jdk.incubator.concurrent is not visible
. . .许多人在这方面帮助了我,显然他们可以让它工作,但由于某些原因,我无法让mvn compile工作。
但是,我可以让代码在IntelliJ下编译和运行。以前,当我能够让IntelliJ编译时,我从未能够让Maven编译。通常情况下,情况正好相反。
发布于 2022-08-07 03:09:17
module-info.java文件
我不明白所有的移动部件,但我确实成功地访问了新的https://wiki.openjdk.org/display/loom/Main特性,虚拟线程 & 结构化并发,分别是预演和孵育。
这是我的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;跑步时:
Eventid=de316ac-10b1-41ed-bfc6-732c4e184566,时间=2022-08-07T03:04:48.207650Z,reading=9
我在src/main/java的包层次结构之外添加了一个src/main/java文件。
module LoomEx {
requires jdk.incubator.concurrent;
}还有我的POM。我从挂牌的挂牌原型开始。然后,我将所有版本号更新为最新版本。
<?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>我尝试使用从这、这和这等页面获取的各种Maven POM元素,以及来自https://openjdk.org/jeps/11和https://openjdk.org/jeps/428的提示,希望不需要module-info.java文件。但我不能让它起作用。添加模块信息文件是我成功的唯一途径。
当然,我还必须设置通常的多个IntelliJ隐藏在不同地方的多个奇怪设置,以指定Java 19的使用。有关这些麻烦设置的说明,请参阅其他堆栈溢出问题。(把我逼疯了。肯定是IntelliJ中最烦人的问题/缺陷。为什么IntelliJ不能从Maven POM或Gradle设置中读取Java并完成它呢?)
发布于 2022-08-08 07:05:15
我设法使它与Maven一起工作,而不需要一个module-info.java文件。我的POM和您的POM之间的主要区别似乎在于我如何设置编译器参数。而不是你所拥有的:
<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>注意,<arg>元素嵌套在<compilerArgs>中。我在--add-modules参数中也有--add-modules,但我不知道这是否是绝对必要的。而且,--enable-preview似乎只需要在运行时,而不是编译时。我所设置的其他参数(例如,<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] ------------------------------------------------------------------------发布于 2022-08-07 09:34:24
一个小小的添加@BasilBourque优秀的答案。对于那些可能不使用Maven或模块的人,您不需要将您的应用程序变成一个模块。--enable-preview --add-modules jdk.incubator.concurrent的参数在所有启动程序中都是必需的,无论是直接还是通过Maven。
您应该能够使用标准的类路径运行他从命令行提供的并发示例,而没有使用下面的源代码启动程序或javac/java的模块-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 );
}
}
}https://stackoverflow.com/questions/73229247
复制相似问题