Create DynamicThreadPoolExecutor, allowing a cached thread pool while maintaining a maximum amount of threads in the pool and queuing tasks

This commit is contained in:
Vankka 2023-07-20 23:57:38 +03:00
parent 061ab08e2d
commit b7aff53b1d
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
4 changed files with 64 additions and 2 deletions

View File

@ -576,6 +576,23 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
@OverridingMethodsMustInvokeSuper
protected void disable() {
scheduler().run(() -> {
System.out.println(1);
try {
Thread.sleep(500000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
scheduler().run(() -> {
System.out.println(2);
try {
Thread.sleep(500000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
Status status = this.status.get();
if (status == Status.INITIALIZED || status.isShutdown()) {
// Hasn't started or already shutting down/shutdown

View File

@ -25,6 +25,7 @@ import com.discordsrv.common.linking.requirelinking.requirement.*;
import com.discordsrv.common.linking.requirelinking.requirement.parser.RequirementParser;
import com.discordsrv.common.module.type.AbstractModule;
import com.discordsrv.common.scheduler.Scheduler;
import com.discordsrv.common.scheduler.executor.DynamicCachingThreadPoolExecutor;
import com.discordsrv.common.scheduler.threadfactory.CountingThreadFactory;
import java.util.ArrayList;
@ -54,7 +55,7 @@ public abstract class RequiredLinkingModule<T extends DiscordSRV> extends Abstra
@Override
public void enable() {
executor = new ThreadPoolExecutor(
executor = new DynamicCachingThreadPoolExecutor(
1,
Math.max(2, Runtime.getRuntime().availableProcessors() / 2),
10,

View File

@ -22,6 +22,7 @@ import com.discordsrv.api.event.bus.EventPriority;
import com.discordsrv.api.event.bus.Subscribe;
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.scheduler.executor.DynamicCachingThreadPoolExecutor;
import com.discordsrv.common.scheduler.threadfactory.CountingForkJoinWorkerThreadFactory;
import com.discordsrv.common.scheduler.threadfactory.CountingThreadFactory;
import org.jetbrains.annotations.NotNull;
@ -39,7 +40,7 @@ public class StandardScheduler implements Scheduler {
public StandardScheduler(DiscordSRV discordSRV) {
this(
discordSRV,
new ThreadPoolExecutor(
new DynamicCachingThreadPoolExecutor(
/* Core pool size */
1,
/* Max pool size: cpu cores - 2 or at least 4 */

View File

@ -0,0 +1,43 @@
package com.discordsrv.common.scheduler.executor;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.*;
/**
* A {@link ThreadPoolExecutor} that acts like a {@link Executors#newCachedThreadPool()} with a max pool size while allowing queueing tasks.
*/
public class DynamicCachingThreadPoolExecutor extends ThreadPoolExecutor {
private int corePoolSize;
public DynamicCachingThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
@NotNull TimeUnit unit,
@NotNull BlockingQueue<Runnable> workQueue,
@NotNull ThreadFactory threadFactory
) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
this.corePoolSize = corePoolSize;
}
@Override
public int getCorePoolSize() {
return corePoolSize;
}
@Override
public void setCorePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
super.setCorePoolSize(this.corePoolSize);
}
@Override
public synchronized void execute(@NotNull Runnable command) {
super.setCorePoolSize(getMaximumPoolSize());
super.execute(command);
super.setCorePoolSize(this.corePoolSize);
}
}