refactor: 优化代码

This commit is contained in:
2023-10-29 16:40:51 +08:00
parent dc39dfa827
commit 58365c2424
7 changed files with 33 additions and 56 deletions

View File

@@ -1,5 +1,6 @@
package cn.hamster3.mc.plugin.core.common.api;
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
import com.google.gson.Gson;
import lombok.Getter;
import net.kyori.adventure.platform.AudienceProvider;
@@ -9,19 +10,46 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@SuppressWarnings("unused")
public abstract class CoreAPI {
@Getter
protected static CoreAPI instance;
/**
* 异步线程池
*/
@Getter
private final ExecutorService executorService;
/**
* 调度器线程池
*/
@Getter
private final ScheduledExecutorService scheduledService;
public CoreAPI() {
executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
scheduledService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
}
@NotNull
public abstract AudienceProvider getAudienceProvider();
/**
* 获取 HamsterCore 公用数据库连接池
*
* @return 公用数据库连接池
*/
@NotNull
public abstract DataSource getDataSource();
/**
* 获取 HamsterCore 公用数据库连接
*
* @return 公用数据库连接
* @throws SQLException -
*/
@NotNull
public Connection getConnection() throws SQLException {
return getDataSource().getConnection();
@@ -39,15 +67,4 @@ public abstract class CoreAPI {
@NotNull
public abstract Gson getHumanGson();
/**
* @return 异步线程池
*/
@NotNull
public abstract ExecutorService getExecutorService();
/**
* @return 调度器线程池
*/
@NotNull
public abstract ScheduledExecutorService getScheduledExecutorService();
}

View File

@@ -23,7 +23,7 @@ public abstract class CountdownThread implements Runnable {
}
public void start(long initialDelay) {
future = CoreAPI.getInstance().getScheduledExecutorService()
future = CoreAPI.getInstance().getScheduledService()
.scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS);
}

View File

@@ -36,10 +36,10 @@ public final class CoreUtils {
@Deprecated
public static ExecutorService WORKER_EXECUTOR = CoreAPI.getInstance().getExecutorService();
/**
* @deprecated 使用 {@link CoreAPI#getScheduledExecutorService()}
* @deprecated 使用 {@link CoreAPI#getScheduledService()}
*/
@Deprecated
public static ScheduledExecutorService SCHEDULED_EXECUTOR = CoreAPI.getInstance().getScheduledExecutorService();
public static ScheduledExecutorService SCHEDULED_EXECUTOR = CoreAPI.getInstance().getScheduledService();
private CoreUtils() {
}