1 Star 8 Fork 3

dwing/sharecode

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Prime3b.java 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
dwing 提交于 2024-09-18 20:09 +08:00 . update jdk to 23
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import jdk.internal.vm.Continuation;
import jdk.internal.vm.ContinuationScope;
import util.UnsafeUtil;
// javac --add-exports java.base/jdk.internal.vm=ALL-UNNAMED Prime3b.java
// java --add-opens java.base/jdk.internal.vm=ALL-UNNAMED Prime3b
public class Prime3b {
private static final long CONT_OFFSET;
private static final MethodHandle mhYield0, mhEnterSpecial;
private static final ContinuationScope cs = new ContinuationScope("VirtualThreads");
private static final Continuation[] readyStack = new Continuation[10000];
private static int readyStackSize;
static {
try {
var lookup = MethodHandles.lookup();
CONT_OFFSET = UnsafeUtil.objectFieldOffset(Thread.class, "cont");
mhYield0 = lookup.unreflect(UnsafeUtil.getMethod(Continuation.class, "yield0",
ContinuationScope.class, Continuation.class));
mhEnterSpecial = lookup.unreflect(UnsafeUtil.getMethod(Continuation.class, "enterSpecial",
Continuation.class, boolean.class, boolean.class));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("removal")
static void loop() {
try {
var t = Thread.currentThread();
var s = readyStack;
for (int n; (n = readyStackSize) > 0; ) {
var c = s[--n];
s[n] = null;
readyStackSize = n;
UnsafeUtil.unsafe.putObject(t, CONT_OFFSET, c);
mhEnterSpecial.invokeExact(c, true, false);
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
static void go(Runnable r) {
new Continuation(cs, r).run();
}
static final class IntChan {
private Continuation waitC;
private int value;
private boolean hasValue;
@SuppressWarnings("removal")
private void pause() {
var c = (Continuation)UnsafeUtil.unsafe.getObject(Thread.currentThread(), CONT_OFFSET);
waitC = c;
try {
@SuppressWarnings("unused")
var __ = (boolean)mhYield0.invokeExact(c, cs, (Continuation)null);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
private void tryResume() {
var c = waitC;
if (c != null) {
waitC = null;
int n = readyStackSize;
readyStack[n] = c;
readyStackSize = n + 1;
}
}
void put(int v) {
if (hasValue)
pause();
value = v;
hasValue = true;
tryResume();
}
int get() {
if (!hasValue)
pause();
hasValue = false;
tryResume();
return value;
}
}
private static void generate(IntChan ch) {
//noinspection InfiniteLoopStatement
for (int i = 2; ; i++)
ch.put(i);
}
private static void filter(IntChan in, IntChan out, int prime) {
//noinspection InfiniteLoopStatement
for (; ; ) {
var i = in.get();
if (i % prime != 0)
out.put(i);
}
}
public static void main(String[] args) {
var count = args.length > 0 ? Integer.parseInt(args[0]) : 10000;
var ch = new IntChan();
go(() -> generate(ch));
go(() -> {
var ch0 = ch;
for (int i = 0; ; ) {
var prime = ch0.get();
if (++i == count) {
System.out.println(prime);
System.exit(0);
}
var ch1 = ch0;
var ch2 = new IntChan();
go(() -> filter(ch1, ch2, prime));
ch0 = ch2;
}
});
loop();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/dwing/sharecode.git
git@gitee.com:dwing/sharecode.git
dwing
sharecode
sharecode
main

搜索帮助