这个问题不是关于它们之间的区别 - 我知道什么是虚假故障以及为什么它会发生在LL/SC上.我的问题是,如果我使用的是intel x86并使用java-9(build 149),为什么它们的汇编代码有区别?
public class WeakVsNonWeak {
static jdk.internal.misc.Unsafe UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Holder h = new Holder();
h.setValue(33);
Class<?> holderClass = Holder.class;
long valueOffset = UNSAFE.objectFieldOffset(holderClass.getDeclaredField("value"));
int result = 0;
for (int i = 0; i < 30_000; ++i) {
result = strong(h, valueOffset);
}
System.out.println(result);
}
private static int strong(Holder h, long offset) {
int sum = 0;
for (int i = 33; i < 11_000; ++i) { …Run Code Online (Sandbox Code Playgroud) 我已经创建了我非常确定的模块化jar文件.但如果可能的话,我想仔细检查一下.给定一个jar文件,有没有办法确定编译器会在其中找到哪些模块?
我正在将项目从JAVA 8迁移到JAVA 9,我在使代码工作时遇到了一些麻烦.所有工作在JAVA 8但在9我有以下错误:
Error java: reference to ok is ambiguous
both method <T>ok(java.util.function.Supplier<T>) and method ok(web.Procedure) match
Run Code Online (Sandbox Code Playgroud)
这是我调用方法时的代码:
public ResponseEntity<List<MailTemplateDto>> mailTemplateFindAll() {
return ok(() -> mailTemplateService.findAll());
}
Run Code Online (Sandbox Code Playgroud)
这是实施:
public <T> ResponseEntity<T> ok(Supplier<T> action) {
return this.body(HttpStatus.OK, action);
}
public <T> ResponseEntity<T> ok(T body) {
return this.ok(() -> {
return body;
});
}
public ResponseEntity<Void> ok(Procedure action) {
action.invoke();
return this.status(HttpStatus.OK);
}
public ResponseEntity<Void> ok() {
return this.status(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
Procedure接口的代码:
@FunctionalInterface
public interface Procedure {
void invoke();
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
可重现的代码 …
我正在尝试使用Spring Boot 2进行Java 10开发,但我遇到了一些问题.
该应用程序是一个基于Spring Boot 2的简单webapp.应用程序启动是可以的但是当我停止它时,我收到此警告:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoaderBase (file:/C:/Users/CS/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.11/tomcat-embed-core-9.0.11.jar) to field java.io.ObjectStreamClass$Caches.localDescs
WARNING: Please consider reporting this to the maintainers of org.apache.catalina.loader.WebappClassLoaderBase
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经将嵌入式Tomcat服务器从版本8切换到9.0.11以符合Java模块系统.该应用程序随选项启动--add-opens java.base/java.lang=ALL-UNNAMED
有人知道我为什么收到这条消息吗?
我只需要针对特定条件执行转换.我做这个转变:
// filter 1: less date - group by max date by groupId
List<Info> listResult = new ArrayList<>(listInfo.stream()
.filter(info -> info.getDate().getTime() < date.getTime())
.collect(Collectors.groupingBy(Info::getGroupId, Collectors.collectingAndThen(
Collectors.reducing((Info i1, Info i2) -> i1.getDate().getTime() > i2.getDate().getTime() ? i1 : i2),
Optional::get))).values());
Run Code Online (Sandbox Code Playgroud)
但是对于超过指定日期的情况,我不需要转换任何东西,我只需要返回这些数据:
// filter 2: more date - nothing change in list
List<Info> listMoreByDate = listInfo.stream()
.filter(info -> info.getDate().getTime() >= date.getTime())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
接下来,要结合这两个过滤器 - 我将两个列表组合在一起:
listResult.addAll(listMoreByDate);
Run Code Online (Sandbox Code Playgroud)
我的问题是,这可以在一个流中完成吗?因为过滤器2绝对没用,它只返回这个条件的列表.
是否可以通过一个连续表达式执行这些转换?
我的完整代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
public class App {
public static …Run Code Online (Sandbox Code Playgroud) Stream<String> a = Stream.of("one", "three", "five");
Stream<String> b = Stream.of("two", "four", "six");
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能使输出如下?
// one
// two
// three
// four
// five
// six
Run Code Online (Sandbox Code Playgroud)
我调查了concat但是正如javadoc解释的那样,它只是一个接一个地追加,它没有交错/散布.
Stream<String> out = Stream.concat(a, b);
out.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
创建一个延迟连接的流,其元素是第一个流的所有元素,后跟第二个流的所有元素.
错误地给出了
// one
// three
// five
// two
// four
// six
Run Code Online (Sandbox Code Playgroud)
可以这样做,如果我收集它们并迭代,但希望更多Java8-y,Streamy :-)
注意
我不想压缩流
"zip"操作将从每个集合中获取一个元素并将它们组合在一起.
拉链操作的结果将是这样的:(不需要的)
// onetwo
// threefour
// fivesix
Run Code Online (Sandbox Code Playgroud) 试用JDK/12 EarlyAccess Build 20,其中JEP-325 Switch Expressions已作为预览功能集成.表达式的示例代码(如在JEP中一样):
Scanner scanner = new Scanner(System.in);
Day day = Day.valueOf(scanner.next().toUpperCase());
int i = switch (day) {
case MONDAY,TUESDAY, WEDNESDAY:
break 0;
default:
System.out.println("Second half of the week");
// ERROR! Group doesn't contain a break with value
};
Run Code Online (Sandbox Code Playgroud)
我试图按照上一个问题中所述的相同步骤,如何使用Maven编译JDK12预览功能并使用命令行执行上面的代码块:
java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
有点像我的期望我得到以下错误:
Run Code Online (Sandbox Code Playgroud)Error: Unable to initialize main class com.stackoverflow.nullpointer.expression.SwitchExpressionMustComplete Caused by: java.lang.VerifyError: Bad local variable type Exception Details: Location: com/stackoverflow/nullpointer/expression/SwitchExpressionMustComplete.main([Ljava/lang/String;)V @66: iload Reason: Type top (current frame, locals[4]) …
我正在使用使用 Java 11 构建的微服务,该服务具有在 Java 8 中构建的依赖项。依赖项中有 rest-clients,并且有一种方法可以执行此操作:
public <T> ResponseEntity<my.company.Wrapper<T>> getForList(String url, HttpHeaders headers) {
try {
return template.exchange(url, GET, new HttpEntity<>(headers), new ParameterizedTypeReference<Wrapper<T>>() { });
} catch (RestClientException restEx) {
logger.error("RestClientException!, restEx: {}", restEx);
throw restEx;
}
}
Run Code Online (Sandbox Code Playgroud)
休息调用刚刚成功,
response -> <200,Wrapper [ elements = [{id=dbfc2557-1738-45e4-8ecd-235d35158957, .....
然后,在微服务(使用 Java 11)中,当它使用 java Stream 时有一点:
var found = enttsResponse.stream()
.map(MyCompanyResponse::getId)
.collect( toList() );
Run Code Online (Sandbox Code Playgroud)
异常消息是:
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.mycompany.commons.v2.entitlement.MyCompanyResponse (java.util.LinkedHashMap is in module java.base of loader …Run Code Online (Sandbox Code Playgroud) 这令人惊讶:我能够用该名称声明一个变量,record即使它现在已成为关键字。看看这个:
public class Main {
static class Foo {
void bar() { System.out.println("Foo.bar"); }
}
record R (int a) {}
public static void main(String[] args) {
Foo record = new Foo();
record.bar();
R r = new R(5);
System.out.println(r);
}
}
Run Code Online (Sandbox Code Playgroud)
当使用 Java 17 编译并运行时,会给出:
Foo.bar
R[a=5]
Run Code Online (Sandbox Code Playgroud)
我原以为这会导致错误,就像尝试声明名为 的变量时的情况一样class。据我所知,Java 人员非常小心,不破坏现有代码,因此我认为这可能是一个经过深思熟虑的选择。
(你甚至不能声明一个名为 name 的变量,const因为它const 是Java 中的关键字。)
一个人如何避免明确地Exception试图从一个Optional或同时使用中获取价值Optional.get?
目前,API可能会受到以下保护orElseThrow:
// exception could be replaced with any other
Integer opt = anyOddInStream.orElseThrow(
() -> new NoSuchElementException("No value present"));
Run Code Online (Sandbox Code Playgroud)
然而,get当试图访问类似的东西时,直接实现令人失望(当一个人可能不愿意显式抛出异常时)
// the following not only just fails but throws an exception as well
Integer previous = anyOddInStream.get();
Run Code Online (Sandbox Code Playgroud)
如果一个人想要确保其中Optional一个有值,如果没有,他们不想继续null向前传播怎么办?
java ×10
java-8 ×4
java-9 ×3
java-10 ×2
java-module ×2
java-stream ×2
java-11 ×1
java-12 ×1
java-17 ×1
java-record ×1
javac ×1
jls ×1
jvm ×1
jvm-hotspot ×1
optional ×1
spring-boot ×1
tomcat9 ×1