import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.atomic.AtomicInteger;import jdk.internal.vm.Continuation;import jdk.internal.vm.ContinuationScope;// java --add-opens java.base/jdk.internal.vm=ALL-UNNAMED LoomBugpublic final class LoomBug {private static final ContinuationScope cs = new ContinuationScope("VirtualThreads");private static final ForkJoinPool pool = new ForkJoinPool();private static final ConcurrentLinkedQueue<CoTask> queue = new ConcurrentLinkedQueue<>();private static final AtomicInteger sum = new AtomicInteger();private static final AtomicInteger finish = new AtomicInteger();private static final AtomicInteger a = new AtomicInteger();static final class CoTask extends Continuation {private final Runnable task;private CoTask(Runnable target) {super(cs, target);task = () -> {run();if (!isDone())queue.offer(this);};}}static void go(Runnable r) {pool.submit(new CoTask(r).task);}static void executeQueue() {for (int i = 0; i < 100; i++) {var coTask = queue.poll();if (coTask != null)pool.submit(coTask.task);}}public static void main(String[] args) throws InterruptedException {go(() -> {//noinspection InfiniteLoopStatementfor (; ; ) {for (int i = 0; i < 100; i++) {var coTask = queue.poll();if (coTask != null)pool.submit(coTask.task);}try {//noinspection BusyWaitThread.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}}});var n = pool.getParallelism();for (int i = 0; i < n; i++) {go(() -> {for (int j = 0; j < 1000; j++) {synchronized (LoomBug.class) {if (a.get() > 0)System.out.println("ERR");a.incrementAndGet();Continuation.getCurrentContinuation(cs);try {Thread.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}a.decrementAndGet();}// executeQueue();Continuation.yield(cs);sum.incrementAndGet();}finish.incrementAndGet();});}for (; ; ) {System.out.println(sum.get() + ", " + finish.get() + ", " + a.get());if (sum.get() == n * 1000) {System.out.println("done!");System.exit(0);}//noinspection BusyWaitThread.sleep(1000);}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。