Added the ability to create a private thread.

This commit is contained in:
Brianna 2020-03-25 09:06:41 -04:00
parent 1b17a8f24c
commit 4079893ea4

View File

@ -7,12 +7,18 @@ import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class DataManagerAbstract { public class DataManagerAbstract {
protected final DatabaseConnector databaseConnector; protected final DatabaseConnector databaseConnector;
protected final Plugin plugin; protected final Plugin plugin;
private static Map<String, ScheduledExecutorService> threads = new HashMap<>();
public DataManagerAbstract(DatabaseConnector databaseConnector, Plugin plugin) { public DataManagerAbstract(DatabaseConnector databaseConnector, Plugin plugin) {
this.databaseConnector = databaseConnector; this.databaseConnector = databaseConnector;
this.plugin = plugin; this.plugin = plugin;
@ -61,4 +67,15 @@ public class DataManagerAbstract {
public void sync(Runnable runnable) { public void sync(Runnable runnable) {
Bukkit.getScheduler().runTask(this.plugin, runnable); Bukkit.getScheduler().runTask(this.plugin, runnable);
} }
/**
* Queue a task to be run synchronously on a new thread.
*
* @param runnable task to run on the next server tick
* @param threadKey the thread key to run on.
*/
public void sync(Runnable runnable, String threadKey) {
threads.computeIfAbsent(threadKey.toUpperCase(),
t -> Executors.newSingleThreadScheduledExecutor()).execute(runnable);
}
} }