Project Loom现在在Java16的special early-release builds中可用。
如果我要在Project Loom技术所缺乏的Java实现上运行我的基于Loom的应用程序,有没有办法在应用程序启动的早期检测到这一点?
我想写这样的代码:
if( projectLoomIsPresent() )
{
… proceed …
}
else
{
System.out.println( "ERROR - Project Loom technology not present." ) ;
}如何实现projectLoomIsPresent()方法?
发布于 2020-12-17 12:58:15
方法1:
return System.getProperty("java.version").contains("loom");方法二:
try {
Thread.class.getDeclaredMethod("startVirtualThread", Runnable.class);
return true;
} catch (NoSuchMethodException e) {
return false;
}发布于 2020-12-17 09:07:12
您可以检查Project Loom之前不存在的特征:
import java.util.Arrays;
public static boolean projectLoomIsPresent() {
return Arrays.stream(Thread.class.getClasses())
.map(Class::getSimpleName)
.anyMatch(name -> name.equals("Builder"));
}不需要捕获异常:
import java.lang.reflect.Method;
import java.util.Arrays;
public static boolean projectLoomIsPresent() {
return Arrays.stream(Thread.class.getDeclaredMethods())
.map(Method::getName)
.anyMatch(name -> name.equals("startVirtualThread"));
}https://stackoverflow.com/questions/65333111
复制相似问题