Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from dwing/sharecode
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
main
Branches (1)
main
main
Branches (1)
main
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
main
Branches (1)
main
sharecode
/
java
/
src
/
Prime5.java
sharecode
/
java
/
src
/
Prime5.java
Prime5.java 4.09 KB
Copy Edit Raw Blame History
dwing authored 2023年04月27日 17:18 +08:00 . java: remove "--enable-preview"
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 Prime5
public final class Prime5 { // buggy because ForkJoinPool & Continuation
private 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;
}
@Override
public Void getRawResult() {
return null;
}
@Override
protected void setRawResult(Void value) {
}
@Override
public void run() {
exec();
}
@Override
protected boolean exec() {
try {
do
coTask.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;
} else
waitHead = c.waitNext;
}
}
if (c != null) {
c.chanValue = v;
c.resume();
} else
CoTask.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 InfiniteLoopStatement
for (int i = 2; ; i++)
ch.send(i);
}
static void filter(IntChan in, IntChan out, int prime) {
//noinspection InfiniteLoopStatement
for (; ; ) {
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);
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

分享个人编写的各种编程语言的短代码
No labels
MIT
Use MIT
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
other
1
https://gitee.com/hzqd/sharecode.git
git@gitee.com:hzqd/sharecode.git
hzqd
sharecode
sharecode
main
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /