Skip to content

Commit

Permalink
add: thread usages
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Dec 21, 2024
1 parent 4ccff7e commit bf8bc6e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
8 changes: 8 additions & 0 deletions fundamentals/src/ThreadSamples/MyThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ThreadSamples;

public class MyThread extends Thread {
@Override
public void run() {
System.out.println("myThread is running");
}
}
12 changes: 10 additions & 2 deletions fundamentals/src/ThreadSamples/ThreadSamples.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package ThreadSamples;

/**
* Main Class for Thread in Java Samples and Usages
*/
public class ThreadSamples {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
System.out.println("==> Thread Samples");

TimerTaskSample timerTaskSample = new TimerTaskSample();
timerTaskSample.SampleOne();
timerTaskSample.SampleTwo();

ThreadUsages threadUsages = new ThreadUsages();
threadUsages.SampleOne();
// threadUsages.ThreadSleepSample();
threadUsages.MyThreadSample();
}

}
42 changes: 42 additions & 0 deletions fundamentals/src/ThreadSamples/ThreadUsages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ThreadSamples;

public class ThreadUsages {
public void SampleOne() {
System.out.println("\n===> Thread Usage Sample One ");

System.out.println("Thread Active Count --> " + Thread.activeCount());

Thread.currentThread().setName("MAINNNNNN");

System.out.println("Current Thread Name --> " + Thread.currentThread().getName());

System.out.println("Current Thread Priority --> " + Thread.currentThread().getPriority());

System.out.println("Is Alive ? --> " + Thread.currentThread().isAlive());
}

public void ThreadSleepSample() throws InterruptedException {
System.out.println("\n===> Thread Sleep Sample");

for (int i = 3; i > 0; i--) {
System.out.println(i);
Thread.sleep(1000);
}
System.out.println("Thread sleep done!");
}

public void MyThreadSample() {
System.out.println("\n===> My Thread Sample");
MyThread myThread = new MyThread();

myThread.start();

System.out.println("myThread is alive --> " + myThread.isAlive());

myThread.setName("MY_THREAD");

System.out.println("myThread name --> " + myThread.getName());

System.out.println("myThread priority --> " + myThread.getPriority());
}
}
2 changes: 1 addition & 1 deletion fundamentals/src/ThreadSamples/TimerTaskSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void run() {
System.out.println("Task is complete (Sample One)!");
}
};
timer.schedule(task, 3000);
timer.schedule(task, 0);
}

public void SampleTwo() {
Expand Down

0 comments on commit bf8bc6e

Please sign in to comment.