refactor: 优化代码
This commit is contained in:
@@ -78,7 +78,7 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
logger.info("已关闭数据库连接池.");
|
||||
CoreAPI.getInstance().getExecutorService().shutdownNow();
|
||||
logger.info("已关闭线程池.");
|
||||
CoreAPI.getInstance().getScheduledExecutorService().shutdownNow();
|
||||
CoreAPI.getInstance().getScheduledService().shutdownNow();
|
||||
logger.info("已关闭调度器.");
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
InventoryView view = player.getOpenInventory();
|
||||
|
@@ -5,7 +5,6 @@ import cn.hamster3.mc.plugin.core.bukkit.util.serializer.ItemStackAdapter;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.serializer.PotionEffectAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import com.google.gson.Gson;
|
||||
@@ -20,17 +19,11 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreBukkitAPI extends CoreAPI {
|
||||
private final Gson gson;
|
||||
private final Gson humanGson;
|
||||
private final HikariDataSource datasource;
|
||||
private final ExecutorService executorService;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
private CoreBukkitAPI() {
|
||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||
@@ -72,9 +65,6 @@ public final class CoreBukkitAPI extends CoreAPI {
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
}
|
||||
|
||||
public static CoreBukkitAPI getInstance() {
|
||||
@@ -107,14 +97,4 @@ public final class CoreBukkitAPI extends CoreAPI {
|
||||
public @NotNull Gson getHumanGson() {
|
||||
return humanGson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ExecutorService getExecutorService() {
|
||||
return executorService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ScheduledExecutorService getScheduledExecutorService() {
|
||||
return scheduledExecutorService;
|
||||
}
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ public class HamsterCorePlugin extends Plugin {
|
||||
logger.info("已关闭数据库连接池.");
|
||||
CoreAPI.getInstance().getExecutorService().shutdownNow();
|
||||
logger.info("已关闭线程池.");
|
||||
CoreAPI.getInstance().getScheduledExecutorService().shutdownNow();
|
||||
CoreAPI.getInstance().getScheduledService().shutdownNow();
|
||||
logger.info("已关闭调度器.");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms.");
|
||||
|
@@ -4,7 +4,6 @@ import cn.hamster3.mc.plugin.core.bungee.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import com.google.gson.Gson;
|
||||
@@ -16,17 +15,11 @@ import net.kyori.adventure.text.Component;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreBungeeAPI extends CoreAPI {
|
||||
private final Gson gson;
|
||||
private final Gson humanGson;
|
||||
private final HikariDataSource datasource;
|
||||
private final ExecutorService executorService;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
private CoreBungeeAPI() {
|
||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||
@@ -64,9 +57,6 @@ public final class CoreBungeeAPI extends CoreAPI {
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
}
|
||||
|
||||
public static CoreBungeeAPI getInstance() {
|
||||
@@ -99,14 +89,4 @@ public final class CoreBungeeAPI extends CoreAPI {
|
||||
public @NotNull Gson getHumanGson() {
|
||||
return humanGson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ExecutorService getExecutorService() {
|
||||
return executorService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ScheduledExecutorService getScheduledExecutorService() {
|
||||
return scheduledExecutorService;
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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() {
|
||||
}
|
||||
|
Reference in New Issue
Block a user