Class KThread
java.lang.Object
java.lang.Thread
ch.k43.util.KThread
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
KLogSMTPHandlerThread
This class adds some convenient methods for starting and stopping Java threads.
Example: Start/stop thread // Create thread and call kStart() method KThread thread = new TestThread(); ... // Signal termination and send interrupt to thread thread.kStop();
Example: User thread public class TestThread extends KThread { public TestThread() { ... } public void kStart() { ... // Run until kStop() is called while (!kMustTerminate()) { ... } } public synchronized void kCleanup() { // Do any resource cleanup (will be called automatically) } }
- Since:
- 2024.09.06
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Method Summary
Modifier and TypeMethodDescriptionvoid
kCleanup()
Cleanup method called by run() just before termination.final boolean
Check if thread should be terminated.abstract void
kStart()
Main entry point for user thread.final void
kStop()
Set the thread termination flag and interrupt the thread.final void
kStop
(boolean argInterrupt) Set the thread termination flag and optionally send interrupt to the thread.final void
run()
Start thread by calling method kStart().final void
start()
Prohibit overwriting.toString()
Output object data.Methods inherited from class java.lang.Thread
activeCount, checkAccess, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, startVirtualThread, stop, threadId, yield
-
Method Details
-
kCleanup
public void kCleanup()Cleanup method called by run() just before termination. -
kMustTerminate
public final boolean kMustTerminate()Check if thread should be terminated.- Returns:
- True (if kStop() was called previously), false otherwise
- See Also:
-
kStart
public abstract void kStart()Main entry point for user thread. This method must be implemented by the subclass. -
kStop
public final void kStop()Set the thread termination flag and interrupt the thread. -
kStop
public final void kStop(boolean argInterrupt) Set the thread termination flag and optionally send interrupt to the thread.- Parameters:
argInterrupt
- Send interrupt signal to thread
-
run
-
start
-
toString
-