Commit 0e44a04
committed
feat: Add ThreadsMethods demo showing thread metadata, sleep, and lifecycle checks
WHAT the code does:
- Adds `MyThread` (extends `Thread`) that:
- Accepts a thread name via `super(name)`.
- Sleeps 1 second at start of `run()` then enters an infinite counter loop printing increasing numbers.
- Adds `ThreadsMethods` main class that:
- Instantiates a `MyThread` named "My Thread 1".
- Prints runtime/thread metadata: `getId()`, `getContextClassLoader()`, `getPriority()`.
- Starts the thread and prints its `getState()` and `isAlive()` status.
- Includes (commented) examples showing where to set priority and how to use Runnable anonymously.
WHY this matters:
- Shows how to create and name threads, and why naming helps debugging.
- Demonstrates thread lifecycle basics: NEW → RUNNABLE → TIMED_WAITING (during sleep) → RUNNABLE.
- Teaches how to inspect thread attributes for debugging or monitoring:
- `getId()` for unique thread id,
- `getPriority()` for scheduler hints,
- `getContextClassLoader()` for class loading context,
- `getState()` and `isAlive()` for runtime state checks.
- Reinforces safe use of `Thread.sleep()` and interruption handling.
HOW it works:
1. `new MyThread("My Thread 1")` creates a thread in state NEW.
2. `t.start()` asks the JVM to schedule the thread; JVM calls `run()` on the new thread.
3. `run()` performs `Thread.sleep(1000)` (TIMED_WAITING) then loops printing a counter.
4. Main thread immediately prints state and alive flag; because of timings the thread may still be NEW, RUNNABLE, or TIMED_WAITING depending on scheduling.
5. Because `MyThread.run()` loops forever, it will keep printing until JVM exits or the thread is interrupted/stopped.
TIPS & GOTCHAS:
- Don’t call `run()` directly — use `start()` to actually spawn a new OS/JVM thread.
- Always handle `InterruptedException` by restoring interrupt status if you cannot fully handle it: `Thread.currentThread().interrupt()`.
- Setting priority (`setPriority`) only hints to the scheduler — behavior is platform-dependent and not guaranteed.
- Avoid infinite loops in production; provide a controlled shutdown using a `volatile boolean running` flag or proper interrupt handling.
- Printing in tight loops can flood the console and distort scheduling observations — use sleep or bounded loops for demos.
- `getContextClassLoader()` useful in modular/classloader-heavy apps; often not required for simple demos.
- For real concurrency tasks prefer `ExecutorService` / thread pools instead of raw thread management.
Short key: java-threads thread-metadata sleep lifecycle getId getState isAlive priority debugging.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 562bac0 commit 0e44a04
1 file changed
+24
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 3 | + | |
| 4 | + | |
13 | 5 | | |
14 | | - | |
| 6 | + | |
| 7 | + | |
15 | 8 | | |
16 | | - | |
17 | | - | |
| 9 | + | |
18 | 10 | | |
19 | | - | |
20 | 11 | | |
| 12 | + | |
21 | 13 | | |
22 | 14 | | |
23 | 15 | | |
24 | | - | |
25 | | - | |
| 16 | + | |
26 | 17 | | |
27 | 18 | | |
28 | 19 | | |
| |||
32 | 23 | | |
33 | 24 | | |
34 | 25 | | |
35 | | - | |
36 | | - | |
| 26 | + | |
37 | 27 | | |
38 | | - | |
39 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
40 | 31 | | |
41 | 32 | | |
42 | 33 | | |
43 | | - | |
44 | 34 | | |
| 35 | + | |
45 | 36 | | |
46 | | - | |
47 | 37 | | |
48 | 38 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 39 | + | |
| 40 | + | |
53 | 41 | | |
| 42 | + | |
54 | 43 | | |
55 | | - | |
| 44 | + | |
| 45 | + | |
56 | 46 | | |
57 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments