Customize thread creation

This commit is contained in:
JeremyStar™ 2024-07-25 04:10:15 +02:00
parent ee40ac5826
commit ef40c04877
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 28 additions and 5 deletions

View file

@ -72,14 +72,26 @@ public final class Engine implements SubsystemMainClass {
@Getter @Getter
private static Engine instance = null; private static Engine instance = null;
/**
* Contains the thread group of the engine.
*
* @since v1-alpha2
*
* -- GETTER --
* Returns the thread group of the engine.
*
* @return engine thread group
* @since v1-alpha2
*/
@Getter
private static final ThreadGroup threadGroup = new ThreadGroup("sos!engine");
/** /**
* Logger instance. * Logger instance.
* *
* @see LoggerInstance * @see LoggerInstance
* @since v1-alpha0 * @since v1-alpha0
*/ */
@SuppressWarnings("NotNullFieldNotInitialized")
@NotNull
private LoggerInstance logger; private LoggerInstance logger;
/** /**

View file

@ -168,7 +168,10 @@ public class EventHelper {
}; };
if (EngineConfiguration.getInstance().isOptimizeEvents()) if (EngineConfiguration.getInstance().isOptimizeEvents())
Thread.ofVirtual().start(eventCode); Thread
.ofVirtual()
.name("Event " + event.getName() + "[" + arguments + "]")
.start(eventCode);
else else
eventCode.run(); eventCode.run();
} }

View file

@ -129,7 +129,7 @@ public final class Logger {
public static void startLoggingThread() { public static void startLoggingThread() {
if (loggingThread == null) { if (loggingThread == null) {
// Logging thread not defined, create and start new one // Logging thread not defined, create and start new one
loggingThread = Thread.ofPlatform().start(() -> { Runnable threadLogic = () -> {
while (true) { // Run in loop while (true) { // Run in loop
// Stop thread when engine is shutting down // Stop thread when engine is shutting down
if (Engine.getInstance().isShuttingDown()) if (Engine.getInstance().isShuttingDown())
@ -144,7 +144,15 @@ public final class Logger {
Thread.sleep(EngineConfiguration.getInstance().getLoggerPollingSpeed()); Thread.sleep(EngineConfiguration.getInstance().getLoggerPollingSpeed());
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
}); };
loggingThread = Thread
.ofPlatform()
.daemon()
.name("Logging thread")
.group(Engine.getThreadGroup())
.stackSize(10)
.start(threadLogic);
} else { } else {
// Restart logging thread if dead // Restart logging thread if dead
if (!loggingThread.isAlive()) if (!loggingThread.isAlive())