Skip to content

Commit

Permalink
Use virtual threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Aug 9, 2024
1 parent dd9f11a commit ee3c628
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 61 deletions.
17 changes: 2 additions & 15 deletions org.jgrapes.core/src/org/jgrapes/core/Components.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import org.jgrapes.core.annotation.ComponentManager;
Expand All @@ -54,17 +53,7 @@
public class Components {

private static ExecutorService defaultExecutorService
= Executors.newCachedThreadPool(
new ThreadFactory() {
@SuppressWarnings({ "PMD.CommentRequired",
"PMD.MissingOverride" })
public Thread newThread(Runnable runnable) {
Thread thread
= Executors.defaultThreadFactory().newThread(runnable);
thread.setDaemon(true);
return thread;
}
});
= Executors.newVirtualThreadPerTaskExecutor();

private static ExecutorService timerExecutorService
= defaultExecutorService;
Expand Down Expand Up @@ -463,9 +452,7 @@ private static class Scheduler extends Thread {
*/
@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
public Scheduler() {
setName("Components.Scheduler");
setDaemon(true);
start();
ofVirtual().name("Components.Scheduler").start(this);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions org.jgrapes.http/test/org/jgrapes/http/test/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ public void testConcurrentGetRoot()
null, null).toURL();

int threadCount = 1000;
if (Boolean.parseBoolean(
System.getenv().getOrDefault("CI", "false"))) {
threadCount = 100;
}

final List<Thread> threads = new ArrayList<>();
AtomicInteger pending = new AtomicInteger(0);
for (int i = 0; i < threadCount; i++) {
Expand Down
7 changes: 2 additions & 5 deletions org.jgrapes.io/src/org/jgrapes/io/InputStreamMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ public void onStart(Start event) {
() -> {
return ByteBuffer.allocateDirect(bufferSize);
}, 2);
runner = new Thread(this, Components.simpleObjectName(this));
// Because this cannot reliably be stopped, it doesn't prevent
// shutdown.
runner.setDaemon(true);
runner.start();
runner = Thread.ofVirtual().name(Components.simpleObjectName(this))
.start(this);
}
}

Expand Down
4 changes: 2 additions & 2 deletions org.jgrapes.io/src/org/jgrapes/io/NioDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public void onStart(Start event) {
if (runner != null && !runner.isInterrupted()) {
return;
}
runner = new Thread(this, Components.simpleObjectName(this));
runner.start();
runner = Thread.ofVirtual().name(Components.simpleObjectName(this))
.start(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public class ManagedBufferStreamer implements InputConsumer {
* @param processor the processor
*/
public ManagedBufferStreamer(Consumer<Reader> processor) {
Thread thread = new Thread(() -> {
Thread thread = Thread.ofVirtual().start(() -> {
processor.accept(reader);
});
thread.start();
ThreadCleaner.watch(this, thread);
}

Expand Down
5 changes: 1 addition & 4 deletions org.jgrapes.io/src/org/jgrapes/io/util/ThreadCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public RefWithThread(Object referent, Thread thread) {
}

static {
Thread watchdog = new Thread(() -> {
Thread.currentThread().setName("ThreadCleaner");
Thread.ofVirtual().name("ThreadCleaner").start(() -> {
while (true) {
try {
ThreadCleaner.RefWithThread ref
Expand All @@ -84,8 +83,6 @@ public RefWithThread(Object referent, Thread thread) {
}
}
});
watchdog.setDaemon(true);
watchdog.start();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions org.jgrapes.io/src/org/jgrapes/net/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class SocketServer extends SocketConnectionManager
private PermitsPool connLimiter;
private Registration registration;
@SuppressWarnings("PMD.SingularField")
private Purger purger;
private Thread purger;
private long minimumPurgeableTime;

/**
Expand All @@ -127,7 +127,6 @@ private class Purger extends Thread implements AvailabilityListener {
*/
public Purger() {
setName(Components.simpleObjectName(this));
setDaemon(true);
}

@Override
Expand Down Expand Up @@ -418,8 +417,7 @@ public void onRegistered(NioRegistration.Completed event)
return;
}
registration = event.event().get();
purger = new Purger();
purger.start();
purger = Thread.ofVirtual().start(new Purger());
fire(new Ready(serverSocketChannel.getLocalAddress()));
return;
}
Expand Down
36 changes: 16 additions & 20 deletions org.jgrapes.util/src/org/jgrapes/util/FileSystemWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,33 +185,29 @@ public DirectorySubscription register(Path toWatch) {
*/
private final class Watcher {
private final WatchService watchService;
private final Thread thread;

private Watcher(FileSystem fileSystem) throws IOException {
watchService = fileSystem.newWatchService();
thread = new Thread(() -> {
while (true) {
try {
WatchKey key = watchService.take();
// Events have to be consumed
key.pollEvents();
if (!(key.watchable() instanceof Path)) {
Thread.ofVirtual().name(fileSystem.toString() + " watcher")
.start(() -> {
while (true) {
try {
WatchKey key = watchService.take();
// Events have to be consumed
key.pollEvents();
if (!(key.watchable() instanceof Path)) {
key.reset();
continue;
}
handleWatchEvent((Path) key.watchable());
key.reset();
continue;
} catch (InterruptedException e) {
logger.log(Level.WARNING, e,
() -> "No WatchKey: " + e.getMessage());
}
handleWatchEvent((Path) key.watchable());
key.reset();
} catch (InterruptedException e) {
logger.log(Level.WARNING, e,
() -> "No WatchKey: " + e.getMessage());
}
}
});
thread.setDaemon(true);
thread.setName(fileSystem.toString() + " watcher");
thread.start();
});
}

}

/**
Expand Down
5 changes: 1 addition & 4 deletions org.jgrapes.util/src/org/jgrapes/util/Password.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Password {
public Password(char[] password) {
synchronized (Password.class) {
if (purger == null) {
purger = new Thread(() -> {
purger = Thread.ofVirtual().name("PasswordPurger").start(() -> {
while (true) {
try {
Reference<? extends Password> passwordRef
Expand All @@ -63,9 +63,6 @@ public Password(char[] password) {
}
}
});
purger.setName("PasswordPurger");
purger.setDaemon(true);
purger.start();
}
}
this.password = password;
Expand Down

0 comments on commit ee3c628

Please sign in to comment.