我没有找到任何清除Java-9 JShell控制台的命令.我也试图通过这个程序清除JShell控制台,但它也不起作用.
import java.io.IOException;
class CLS {
public static void main(String... arg) throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
}
Run Code Online (Sandbox Code Playgroud)
我认为在早期访问中我们还没有可用的功能.有人有个主意吗?
我很难理解java-9中的实现细节ImmutableCollections.SetN; 具体为什么需要两次增加内部数组.
假设你这样做:
Set.of(1,2,3,4) // 4 elements, but internal array is 8
Run Code Online (Sandbox Code Playgroud)
更准确地说,我完全理解为什么这样做(双重扩展)以防万一HashMap- 你从来没有(几乎)想要load_factor成为一个.例如,!=1当条目更好地分散到存储桶时,值可以改善搜索时间.
但是在一个不可变的集合的情况下- 我无法真正说出来.特别是因为选择了内部数组的索引.
让我提供一些细节.首先如何搜索索引:
int idx = Math.floorMod(pe.hashCode() ^ SALT, elements.length);
Run Code Online (Sandbox Code Playgroud)
pe是我们放在集合中的实际值.SALT在启动时只生成32位,每次生成一次JVM(如果需要,这是实际的随机化).elements.length我们的例子是8(4个元素,但这里有8个 - 大小加倍).
这个表达式就像一个负安全的模运算.请注意,选择存储桶时HashMap,例如((n - 1) & hash)中会执行相同的逻辑操作.
因此,如果elements.length is 8对于我们的情况,则此表达式将返回任何小于8的正值(0, 1, 2, 3, 4, 5, 6, 7).
现在剩下的方法:
while (true) {
E ee = elements[idx];
if …Run Code Online (Sandbox Code Playgroud) 我试图理解OSGi应用程序如何在Java 9中工作,假设OSGi包不是JPMS模块(据我所知,仍然没有OSGi包可以同时用于生产的JPMS模块的解决方案).我有几个问题:
Bundle.update()办?捆绑包是否重新加载到未命名的模块?如果我理解错误,请解释主要原则.
在将我们的一个项目迁移到Java 9(build 9 + 181)时,我遇到了一个特殊的问题,在类型推断和java模块相关的某些库中看起来像是一个不正确的实现.我使用的是dropwizard-core(1.1.0)和guice(4.1.0)配置如下:
public class CustomService extends io.dropwizard.Application<CustomServiceConfig> {
public static void main(String[] args) throws Exception {
new CustomService().run(args);
}
// other initializations
@Override
public void run(CustomServiceConfig config, io.dropwizard.setup.Environment environment) throws Exception {
com.google.inject.Injector injector = createInjector(config, environment);
environment.jersey().register(injector.getInstance(SomeResource.class)); //line 45
environment.healthChecks().register("DBHealth", injector.getInstance(HealthCheck.class));
environment.servlets().addFilter("Filter-Name", SomeFilter.class)
.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
private com.google.inject.Injector createInjector(CustomServiceConfig config, Environment environment) {
return com.google.inject.Guice.createInjector(new CustomServiceModule(config, environment));
}
}
Run Code Online (Sandbox Code Playgroud)
public class CustomServiceModule extends com.google.inject.AbstractModule { …Run Code Online (Sandbox Code Playgroud) 将module-info.java文件添加到我的项目后,我的checkstyle插件开始失败:
[错误]无法执行目标org.apache.maven.plugins:maven-checkstyle-plugin:2.17:检查(default-cli)项目电子邮件:在checkstyle配置期间失败:在分析文件/ home/xxx /期间发生NoViableAltException IdeaProjects/blynk服务器/服务器/通知/电子邮件/ src目录/主/ JAVA/module-info.java.意外令牌:模块 - > [帮助1]
我试过了
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value="module\-info\.java$"/>
</module>
Run Code Online (Sandbox Code Playgroud)
然而,它失败了:
[错误]无法执行目标org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check(default-cli)on project blynk:checkstyle配置失败:无法初始化模块BeforeExecutionExclusionFileFilter - 无法实例化'BeforeExecutionExclusionFileFilter'类,也无法将其实例化为com.puppycrawl.tools.checkstyle.checks.annotation.BeforeExecutionExclusionFileFilter
module-info.java在checkstyle期间为maven-checkstyle-plugin 跳过文件的正确方法是什么?
在我的多模块项目中,我module-info.java只创建了几个模块.在编译期间maven-compiler-plugin:3.7.0我得到下一个警告:
[警告]*检测到必需的基于文件名的自动模块.请不要将此项目发布到公共工件库!*
这是什么意思?是因为我只有几个模块,module-info.java而不是整个项目?
考虑以下WorkExperience课程:
public class WorkExperience {
private int year;
private List<Skills> skill;
public WorkExperience(int year, List<Skills> skill) {
this.year = year;
this.skill = skill;
}
//getter setter
}
public class Skills {
private String skills;
public Skills(String skills) {
this.skills = skills;
}
@Override
public String toString() {
return "Skills [skills=" + skills + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
假设我希望按年份按照我的技能进行分组,这就是我们groupBy今年的表现:
public static void main(String[] args) {
List<Skills> skillSet1 = new ArrayList<>();
skillSet1.add(new Skills("Skill-1"));
skillSet1.add(new Skills("Skill-2"));
skillSet1.add(new Skills("Skill-3"));
List<Skills> skillSet2 …Run Code Online (Sandbox Code Playgroud) 在java-9 中,引入了类中的新方法completeOnTimeoutCompletableFuture:
public CompletableFuture<T> completeOnTimeout(T value, long timeout,
TimeUnit unit) {
if (unit == null)
throw new NullPointerException();
if (result == null)
whenComplete(new Canceller(Delayer.delay(
new DelayedCompleter<T>(this, value),
timeout, unit)));
return this;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它在其实现中使用静态 ScheduledThreadPoolExecutor:
static ScheduledFuture<?> delay(Runnable command, long delay,
TimeUnit unit) {
return delayer.schedule(command, delay, unit);
}
Run Code Online (Sandbox Code Playgroud)
哪里
static final ScheduledThreadPoolExecutor delayer;
static {
(delayer = new ScheduledThreadPoolExecutor(
1, new DaemonThreadFactory())).
setRemoveOnCancelPolicy(true);
}
Run Code Online (Sandbox Code Playgroud)
对我来说这是一种非常奇怪的方法,因为它可能成为整个应用程序的瓶颈:唯一一个ScheduledThreadPoolExecutor只有一个线程保留在池中以执行所有可能的CompletableFuture任务?
我在这里错过了什么?
PS它看起来像:
1)这段代码的作者不愿意提取这种逻辑,而是倾向于重用ScheduledThreadPoolExecutor …
java multithreading threadpoolexecutor java-9 completable-future
在Java 9中不推荐使用Class.newInstance:
Run Code Online (Sandbox Code Playgroud)clazz.newInstance()可以替换为
Run Code Online (Sandbox Code Playgroud)clazz.getDeclaredConstructor().newInstance()
问题是getDeclaredConstructor返回任何构造函数而不考虑访问级别.
如果我想替换我的代码中的所有实例(在不同的包/访问级别),我应该使用getConstructor来获取公共构造函数吗?
与指定的parameterTypes匹配的公共构造函数的Constructor对象
或者我不能批量替换所有事件,因为它需要是每个案例(如果存在公共构造函数和/或我是否具有该类的正确访问级别)?
编辑
getDeclaredConstructor:
Run Code Online (Sandbox Code Playgroud)return getConstructor0(parameterTypes, Member.DECLARED);
getConstructor:
Run Code Online (Sandbox Code Playgroud)return getConstructor0(parameterTypes, Member.PUBLIC);
我已经看过几篇在Java 9中简要提到自包含应用程序的在线演示文稿,但我有一个问题需要我解决.
使用新模块系统,您现在只允许包含运行应用程序所需的最少代码.但是,希望运行应用程序的系统是否仍然需要JRE,或者是否可以包含在程序中的基本模块中?
我怀疑它是后者,因为下载最新版Java 的页面(这里)仍显示版本8_151.
TL; DR - 使用Java 9,是否可以创建一个可以在没有安装JRE/Java的系统上执行的自包含可执行文件?
java ×10
java-9 ×10
maven ×3
java-8 ×2
java-module ×2
collections ×1
constructor ×1
java-stream ×1
jshell ×1
maven-3 ×1
maven-plugin ×1
osgi ×1
reflection ×1