Skip to content

Commit

Permalink
switch the shared pool to a normal threadpool and limit max concurren…
Browse files Browse the repository at this point in the history
…t threads

also fix #29
  • Loading branch information
zlainsama committed Feb 21, 2021
1 parent 6877d22 commit bb2e0c8
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/main/java/lain/lib/SharedPool.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package lain.lib;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class SharedPool
{
public final class SharedPool {

private static final ExecutorService thePool = Executors.newWorkStealingPool(ForkJoinPool.getCommonPoolParallelism() + 1);
private static final ExecutorService thePool = new ThreadPoolExecutor(
2,
Math.min(Runtime.getRuntime().availableProcessors() * 4, Short.MAX_VALUE),
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
SharedPool::newWorker);

public static void execute(Runnable command)
{
thePool.execute(command);
private SharedPool() {
throw new Error("NoInstance");
}

private static Thread newWorker(Runnable target) {
Thread thread = new Thread(target, "SharedPoolWorker");
if (!thread.isDaemon())
thread.setDaemon(true);
if (thread.getPriority() != Thread.NORM_PRIORITY)
thread.setPriority(Thread.NORM_PRIORITY);
return thread;
}

private SharedPool()
{
public static void execute(Runnable command) {
thePool.execute(command);
}

}

0 comments on commit bb2e0c8

Please sign in to comment.