Commit f4859fb
committed
feat: demonstrate thread priorities with run, sleep, and start
WHAT the code does:
- Defines ThreadMethod extending Thread, with a constructor to set thread name.
- Overrides run():
- Loops from 1 to 5.
- Prints thread name, its priority, and loop counter.
- Sleeps for 100ms between iterations.
- In main():
- Creates three threads with custom names.
- Sets different priorities: MIN, NORM, MAX.
- Starts all three threads.
WHY this matters:
- Demonstrates key Thread API methods: start(), run(), sleep(), setPriority().
- Shows how thread priority hints the scheduler about execution order (though not guaranteed).
- Highlights concurrent execution and output interleaving.
HOW it works:
1. Three ThreadMethod objects created with names.
2. Priorities assigned:
- l → normal priority.
- m → minimum priority.
- h → maximum priority.
3. start() called for each → JVM invokes run() in separate threads.
4. Each thread:
- Prints status (name, priority, iteration).
- Sleeps briefly to yield CPU, increasing chance of interleaving output.
Key points:
- start() creates new thread of execution.
- run() contains the task logic.
- sleep() pauses current thread without releasing lock.
- setPriority() influences scheduling but does not guarantee order.
Short key: java-threads priorities run-sleep-start demo.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f082dc0 commit f4859fb
1 file changed
+14
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
| 2 | + | |
6 | 3 | | |
7 | 4 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
12 | 8 | | |
13 | 9 | | |
14 | 10 | | |
| |||
17 | 13 | | |
18 | 14 | | |
19 | 15 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
27 | 21 | | |
28 | 22 | | |
29 | 23 | | |
30 | 24 | | |
31 | | - | |
32 | | - | |
| 25 | + | |
33 | 26 | | |
34 | 27 | | |
35 | 28 | | |
| 29 | + | |
36 | 30 | | |
37 | 31 | | |
38 | 32 | | |
| 33 | + | |
39 | 34 | | |
40 | 35 | | |
41 | 36 | | |
42 | 37 | | |
43 | 38 | | |
44 | 39 | | |
45 | | - | |
| 40 | + | |
0 commit comments