This action will force synchronization from dwing/sharecode, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
import java.lang.invoke.MethodHandles;import java.lang.invoke.VarHandle;import java.util.concurrent.Executor;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.ForkJoinTask;import jdk.internal.vm.Continuation;import jdk.internal.vm.ContinuationScope;// java --add-opens java.base/jdk.internal.vm=ALL-UNNAMED Prime5public final class Prime5 { // buggy because ForkJoinPool & Continuationprivate static final ContinuationScope cs = new ContinuationScope("");private static final Executor pool = new ForkJoinPool(); // Executors.newWorkStealingPool(); // ForkJoinPool.commonPool();public static final class Task extends ForkJoinTask<Void> implements Runnable {private final CoTask coTask;public Task(CoTask coTask) {this.coTask = coTask;}@Overridepublic Void getRawResult() {return null;}@Overrideprotected void setRawResult(Void value) {}@Overridepublic void run() {exec();}@Overrideprotected boolean exec() {try {docoTask.run();while (!(boolean)CoTask.pausingHandle.getAndSet(coTask, false));} catch (Throwable e) {e.printStackTrace();}return false;}}public static final class CoTask extends Continuation {static final VarHandle pausingHandle;private final Task task;int chanValue;CoTask waitNext;boolean pausing;static {var l = MethodHandles.lookup();try {pausingHandle = l.findVarHandle(CoTask.class, "pausing", boolean.class);} catch (ReflectiveOperationException e) {throw new RuntimeException(e);}}public static void go(Runnable r) {pool.execute(new CoTask(r).task);}public static void pause() {Continuation.yield(cs);}public void resume() {if (!(boolean)pausingHandle.getAndSet(this, false))pool.execute(task);}private CoTask(Runnable target) {super(cs, target);task = new Task(this);}}public static final class IntChan {private CoTask waitHead;private boolean hasValue;public void send(int v) {CoTask c;synchronized (this) {if (hasValue) {c = (CoTask)Continuation.getCurrentContinuation(cs);c.chanValue = v;c.waitNext = waitHead;c.pausing = true;waitHead = c;c = null;} else {c = waitHead;if (c == null) {c = (CoTask)Continuation.getCurrentContinuation(cs);c.chanValue = v;c.waitNext = null;c.pausing = true;waitHead = c;hasValue = true;c = null;} elsewaitHead = c.waitNext;}}if (c != null) {c.chanValue = v;c.resume();} elseCoTask.pause();}public int recv() {CoTask c;boolean r;synchronized (this) {if (!hasValue) {c = (CoTask)Continuation.getCurrentContinuation(cs);c.waitNext = waitHead;c.pausing = true;waitHead = c;r = false;} else {c = waitHead;if (c == null) {c = (CoTask)Continuation.getCurrentContinuation(cs);c.waitNext = null;c.pausing = true;waitHead = c;hasValue = false;r = false;} else {waitHead = c.waitNext;r = true;}}}if (r) {var v = c.chanValue;c.resume();return v;} else {CoTask.pause();return c.chanValue;}}}static void go(Runnable r) {CoTask.go(r);}static void generate(IntChan ch) {//noinspection InfiniteLoopStatementfor (int i = 2; ; i++)ch.send(i);}static void filter(IntChan in, IntChan out, int prime) {//noinspection InfiniteLoopStatementfor (; ; ) {var i = in.recv();if (i % prime != 0)out.send(i);}}public static void main(String[] args) throws InterruptedException {var count = args.length > 0 ? Integer.parseInt(args[0]) : 10000;go(() -> {var ch = new IntChan();var ch0 = ch;go(() -> generate(ch0));for (int i = 0; ; ) {var prime = ch.recv();if (++i == count) {System.out.println(prime);System.exit(0);}var ch1 = ch;var ch2 = new IntChan();go(() -> filter(ch1, ch2, prime));ch = ch2;}});Thread.sleep(1_000_000L);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。