Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 8 Fork 3

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
/
Prime3.java
sharecode
/
java
/
src
/
Prime3.java
Prime3.java 3.35 KB
Copy Edit Raw Blame History
dwing authored 2024年09月18日 20:09 +08:00 . update jdk to 23
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import jdk.internal.vm.Continuation;
import jdk.internal.vm.ContinuationScope;
import util.UnsafeUtil;
// javac --add-exports java.base/jdk.internal.vm=ALL-UNNAMED Prime3.java
// java --add-opens java.base/jdk.internal.vm=ALL-UNNAMED Prime3
public class Prime3 {
private static final long CONT_OFFSET;
private static final MethodHandle mhDoYield, mhEnterSpecial;
private static final ContinuationScope cs = new ContinuationScope("VirtualThreads");
private static final ArrayList<Continuation> readyStack = new ArrayList<>(10000);
static {
try {
CONT_OFFSET = UnsafeUtil.objectFieldOffset(Thread.class, "cont");
var lookup = MethodHandles.lookup();
var m = UnsafeUtil.getMethod(Continuation.class, "doYield");
mhDoYield = lookup.unreflect(m);
m = UnsafeUtil.getMethod(Continuation.class, "enterSpecial", Continuation.class, boolean.class, boolean.class);
mhEnterSpecial = lookup.unreflect(m);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("removal")
static void loop() {
try {
for (int n; (n = readyStack.size()) > 0; ) {
var c = readyStack.remove(n - 1);
UnsafeUtil.unsafe.putObject(Thread.currentThread(), CONT_OFFSET, c);
mhEnterSpecial.invokeExact(c, true, false);
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
static void resume(Continuation c) {
if (c.isDone())
throw new IllegalStateException("Continuation terminated");
readyStack.add(c);
}
static void go(Runnable r) {
new Continuation(cs, r).run();
}
@SuppressWarnings("removal")
static void pause(Deque<Continuation> queue) {
var c = (Continuation)UnsafeUtil.unsafe.getObject(Thread.currentThread(), CONT_OFFSET);
queue.addLast(c);
try {
@SuppressWarnings("unused")
var __ = (int)mhDoYield.invokeExact();
VarHandle.releaseFence(); // needed to prevent certain transformations by the compiler
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
static class IntChan {
private final ArrayDeque<Continuation> waitQueue = new ArrayDeque<>();
private int value;
private boolean hasValue;
void put(int v) {
if (hasValue)
pause(waitQueue);
value = v;
hasValue = true;
var c = waitQueue.pollFirst();
if (c != null)
resume(c);
}
int get() {
if (!hasValue)
pause(waitQueue);
hasValue = false;
var c = waitQueue.pollFirst();
if (c != null)
resume(c);
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...
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/dwing/sharecode.git
git@gitee.com:dwing/sharecode.git
dwing
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 によって変換されたページ (->オリジナル) /