Skip to content

Commit

Permalink
Use smaller default size for thread pools (#94)
Browse files Browse the repository at this point in the history
fixes #93
  • Loading branch information
kevinherron authored Dec 18, 2024
1 parent 156100d commit 4f0fdb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Thread newThread(Runnable r) {
}
};

EVENT_LOOP = new NioEventLoopGroup(0, threadFactory);
EVENT_LOOP = new NioEventLoopGroup(1, threadFactory);
}

return EVENT_LOOP;
Expand Down
5 changes: 1 addition & 4 deletions modbus/src/main/java/com/digitalpetri/modbus/Modbus.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ public Thread newThread(Runnable r) {
}
};

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
Runtime.getRuntime().availableProcessors(),
threadFactory
);
var executor = new ScheduledThreadPoolExecutor(1, threadFactory);

executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);

Expand Down
12 changes: 10 additions & 2 deletions modbus/src/main/java/com/digitalpetri/modbus/TimeoutScheduler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.digitalpetri.modbus;

import com.digitalpetri.modbus.internal.util.ExecutionQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand All @@ -23,9 +25,10 @@ interface TimeoutHandle {

}

static TimeoutScheduler fromScheduledExecutor(ScheduledExecutorService ses) {
static TimeoutScheduler create(Executor executor, ScheduledExecutorService scheduledExecutor) {
return (task, delay, unit) -> {
final var ref = new AtomicReference<ScheduledFuture<?>>();
final ExecutionQueue queue = new ExecutionQueue(executor);

var handle = new TimeoutHandle() {
@Override
Expand All @@ -44,7 +47,12 @@ public boolean isCancelled() {
};

synchronized (ref) {
ref.set(ses.schedule(() -> task.run(handle), delay, unit));
ScheduledFuture<?> future = scheduledExecutor.schedule(
() -> queue.submit(() -> task.run(handle)),
delay,
unit
);
ref.set(future);
}

return handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public static class Builder {
*/
public ModbusClientConfig build() {
if (timeoutScheduler == null) {
timeoutScheduler = TimeoutScheduler.fromScheduledExecutor(Modbus.sharedScheduledExecutor());
timeoutScheduler = TimeoutScheduler.create(
Modbus.sharedExecutor(),
Modbus.sharedScheduledExecutor()
);
}

return new ModbusClientConfig(
Expand Down

0 comments on commit 4f0fdb0

Please sign in to comment.