Skip to content

Commit f4859fb

Browse files
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

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
public class ThreadMethod extends Thread
2-
{
3-
4-
public ThreadMethod(String name)
5-
{
1+
public class ThreadMethod extends Thread {
2+
public ThreadMethod(String name) {
63
super(name);
74
}
8-
// Thread class ka overridden method. Thread class Methods.
9-
public void run () {
10-
for (int i = 1; i <= 5; i++)
11-
{
5+
// Thread class ka overridden method. Thread class Methods.
6+
public void run () {
7+
for (int i = 1; i <= 5; i++) {
128
/* {
139
String a = "";
1410
for (int j = 0; j < 10000; j++)
@@ -17,29 +13,28 @@ public void run () {
1713
}
1814
}*/
1915

20-
System.out.println(Thread.currentThread().getName() + " - Priority: " + Thread.currentThread().getPriority() + "- count: " + i);
21-
try
22-
{
23-
Thread.sleep(100);
24-
}
25-
catch(Exception e)
26-
{
16+
System.out.println(Thread.currentThread().getName() + " - Priority: " + Thread.currentThread().getPriority() + "- count: " + i);
17+
try {
18+
Thread.sleep(100);
19+
}
20+
catch(Exception e) {
2721

2822
}
2923
}
3024
}
31-
public static void main(String[] args) throws InterruptedException
32-
{
25+
public static void main(String[] args) throws InterruptedException {
3326
ThreadMethod l = new ThreadMethod("Somesh");
3427
ThreadMethod m = new ThreadMethod("Diwan"); //Setting Thread Priority
3528
ThreadMethod h = new ThreadMethod("Unstoppable");
29+
3630
l.setPriority(Thread.NORM_PRIORITY);
3731
m.setPriority(Thread.MIN_PRIORITY);
3832
h.setPriority(Thread.MAX_PRIORITY);
33+
3934
l.start();
4035
m.start();
4136
h.start();
4237
//t1.start();
4338
}
4439
}
45-
// Start run sleep join setPriority
40+
// Start run sleep join setPriority

0 commit comments

Comments
 (0)