Skip to content

Commit 0e44a04

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

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed
Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import javax.naming.Name;
22

3-
/*
4-
class MyRunThread implements Runnable
5-
{
6-
public void run(){} //Dummy Method
7-
}
8-
*/
9-
class MyThread extends Thread
10-
{
11-
public MyThread(String name)
12-
{
3+
class MyThread extends Thread {
4+
public MyThread(String name) {
135
super(name);
14-
//setPriority(Thread.MAX_PRIORITY); //setting a priority to the thread.
6+
//setPriority(Thread.MAX_PRIORITY);
7+
//setting a priority to the thread.
158
}
16-
public void run()
17-
{
9+
public void run() {
1810
int count=1;
19-
//make thread sleep for while
2011

12+
//make thread sleep for a while.
2113
try {
2214
Thread.sleep(1000);
2315
}
24-
catch (InterruptedException e)
25-
{
16+
catch (InterruptedException e) {
2617
System.out.println(e);
2718
}
2819
while(true) {
@@ -32,26 +23,32 @@ public void run()
3223
}
3324

3425
public class ThreadsMethods {
35-
public static void main(String[] args) throws Exception
36-
{
26+
public static void main(String[] args) throws Exception {
3727
/* Thread t=new Thread(new MyRunThread(),"My Name");*/
38-
//Created anonmus object of runnable.
39-
// without any reference we created an anonmuys object.
28+
29+
//Created an anonymous object of runnable.
30+
//Without any reference, we created an anonymous object.
4031

4132
MyThread t=new MyThread("My Thread 1");
4233
//MyThread class ka object.
43-
System.out.println(t.getId());
4434

35+
System.out.println(t.getId());
4536
System.out.println(t.getContextClassLoader()+"ID");
46-
4737
System.out.println(t.getPriority()+ "Name");
4838

49-
t.start(); //states
50-
/* What are the all the sates.
51-
System.out.println("State "+t.getState());*/
52-
39+
t.start();
40+
/* What are the all the sates.
5341
System.out.println("State "+t.getState());
42+
*/
5443

55-
System.out.println("Alive " +t.isAlive());
44+
System.out.println("State "+ t.getState());
45+
System.out.println("Alive " + t.isAlive());
5646
}
5747
}
48+
49+
/*
50+
class MyRunThread implements Runnable
51+
{
52+
public void run(){} //Dummy Method
53+
}
54+
*/

0 commit comments

Comments
 (0)