# Java Thread

### KThread Class Overview

The `KThread` class provides a simple and convenient framework for managing Java threads, focusing on controlled startup, termination, and cleanup processes. It is designed to streamline thread lifecycle management while ensuring resource cleanup before termination.

#### Key Features

* **Thread Termination Signaling**\
  Any thread can invoke the kStop() method to signal a KThread instance to terminate. This method optionally supports interrupting the thread using Thread.interrupt().
* **Termination Check**\
  The kMustTerminate() method allows threads to check if a termination signal has been issued, enabling responsive and graceful shutdowns during execution.
* **Automatic Resource Cleanup**\
  Before the thread terminates, the kCleanup()  method is automatically invoked to handle any necessary resource cleanup tasks. This ensures proper release of resources and avoids potential memory leaks.

#### Usage Scenario

* **Controlled Thread Management**\
  Ideal for applications requiring predictable and safe thread lifecycle control.
* **Resource-Intensive Tasks**\
  Ensures that resources are properly released even in the event of thread termination.

### Example Main Thread

```java
KThread kThread = new TestThread("Hello World");
KLog.info("Thread created and started");

K.waitSeconds(5);

kThread.kStop();
KLog.debug("Thread stopped");
```

### Example Thread

```java
public class TestThread extends KThread {

   private String gStartArgument = null;
	
   public TestThread(String argString) {
      gStartArgument = argString;
   }

   public void kStart() {
      KLog.info("Thread has started with argument " + gStartArgument);
		
      while (!kMustTerminate()) {
         KLog.info("Thread still running ...");
         K.waitSeconds(1);
      }
      KLog.info("Thread has terminated");
   }
	
   public synchronized void kCleanup() {
      KLog.info("Thread cleanup called");
   }
}
```

<details>

<summary>Info Log</summary>

```
2024-09-05T14:42:52.629 I main[1]:Test:main:9                                          Start
2024-09-05T14:42:52.629 I main[1]:Test:thread:20                                       Thread created and started
2024-09-05T14:42:52.630 I T-0[15]:TestThread:kStart:15                                 Thread has started with argument Hello World
2024-09-05T14:42:52.630 I T-0[15]:TestThread:kStart:18                                 Thread still running ...
2024-09-05T14:42:53.636 I T-0[15]:TestThread:kStart:18                                 Thread still running ...
2024-09-05T14:42:54.641 I T-0[15]:TestThread:kStart:18                                 Thread still running ...
2024-09-05T14:42:55.646 I T-0[15]:TestThread:kStart:18                                 Thread still running ...
2024-09-05T14:42:56.654 I T-0[15]:TestThread:kStart:18                                 Thread still running ...
2024-09-05T14:42:57.637 E T-0[15]:ch.k43.util.K:waitThread:1484                        ===> java.lang.InterruptedException: sleep interrupted
2024-09-05T14:42:57.638 I T-0[15]:TestThread:kStart:22                                 Thread has terminated
2024-09-05T14:42:57.639 I T-0[15]:TestThread:kCleanup:27                               Thread cleanup called
```

</details>
