Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fiber refactorings #4

Open
wants to merge 6 commits into
base: java21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ private String getThreadID() {
abstract String getThreadNamePrefix();

public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new Thread(...) call inside the newThread(...) method used by the _PriorityThreadFactory classes

thread.setName(getThreadNamePrefix() + "-" + getThreadID());
thread.setDaemon(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Virtual threads are always daemon threads

Thread thread = Thread.ofVirtual().name(getThreadNamePrefix() + "-", Long.parseLong(getThreadID())).unstarted(runnable);
return thread;
}
}
Expand Down Expand Up @@ -66,11 +64,11 @@ String getThreadNamePrefix() {

private boolean isShutdown = false;
private final ExecutorService lowPriorityPool =
Executors.newCachedThreadPool(new LowPriorityThreadFactory());
Executors.newThreadPerTaskExecutor(new LowPriorityThreadFactory());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API says newVirtualThreadPerTaskExecutor is equivalent to newThreadPerTaskExecutor with a factory producing virtual threads

private final ExecutorService normalPriorityPool =
Executors.newCachedThreadPool(new NormalPriorityThreadFactory());
Executors.newThreadPerTaskExecutor(new NormalPriorityThreadFactory());
private final ExecutorService highPriorityPool =
Executors.newCachedThreadPool(new HighPriorityThreadFactory());
Executors.newThreadPerTaskExecutor(new HighPriorityThreadFactory());
Copy link
Member Author

@bgprudhomme bgprudhomme Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* @return Shared instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ConcurrentReaderWriter(Callable<Void> writer, Callable<Void> reader,

public void run() throws Exception {
for (int i = 0; i < numThreads / 2f; i++) {
new Thread(() -> { // writer thread
Thread.ofVirtual().start(() -> { // writer thread
try {
writer.call();
} catch (Exception e) {
Expand All @@ -43,9 +43,9 @@ public void run() throws Exception {
} finally {
writeCount.incrementAndGet();
}
}).start();
});

new Thread(() -> {
Thread.ofVirtual().start(() -> {
while (true) {
// Spin until we have something to read.
if (writeCount.get() > 0) {
Expand All @@ -62,7 +62,7 @@ public void run() throws Exception {
sleep(1);
}
}
}).start();
});
}

while (readCount.get() < numThreads / 2f ||
Expand Down
Loading