1+ package multithreading ;
2+
3+ public class ThreadClassMethods extends Thread {
4+
5+ // naming of thread
6+ // public ThreadClassMethods(String name) {
7+ // super(name);
8+ // }
9+
10+ @ Override
11+ public void run () {
12+ /* setPriority()
13+ for(int i = 0; i < 5; i++) {
14+ String a = "";
15+ for(int j = 0; j < 10000; j++) {
16+ a += "a";
17+ }
18+ System.out.println(Thread.currentThread().getName() + " - Priority: " + Thread.currentThread().getPriority() + " - count: " + i);
19+ try {
20+ Thread.sleep(100);
21+ } catch (InterruptedException e) {
22+ throw new RuntimeException(e);
23+ }
24+ }
25+ */
26+
27+
28+ /* interrupt()
29+ try {
30+ Thread.sleep(1000);
31+ System.out.println("Thread is running...");
32+ } catch (InterruptedException e) {
33+ System.out.println("Thread interrupted: " + e);
34+ }
35+ */
36+
37+
38+ /* yield()
39+ for(int i = 0; i < 5; i++) {
40+ System.out.println(Thread.currentThread().getName() + " is running");
41+ Thread.yield();
42+ }
43+ */
44+
45+
46+ while (true ) {
47+ System .out .println ("Hello World!" );
48+ }
49+ }
50+
51+ public static void main (String [] args ) throws InterruptedException {
52+ /* setPriority()
53+ ThreadClassMethods l = new ThreadClassMethods("Low Priority Thread");
54+ ThreadClassMethods m = new ThreadClassMethods("Medium Priority Thread");
55+ ThreadClassMethods h = new ThreadClassMethods("High Priority Thread");
56+
57+ l.setPriority(Thread.MIN_PRIORITY);
58+ m.setPriority(Thread.NORM_PRIORITY);
59+ h.setPriority(Thread.MAX_PRIORITY);
60+
61+ // 1. start() -> when it's called, JVM will execute run method
62+ l.start();
63+ m.start();
64+ h.start();
65+ */
66+
67+
68+
69+ /* interrupt()
70+ ThreadClassMethods t1 = new ThreadClassMethods();
71+ t1.start();
72+ t1.interrupt(); // Thread interrupted: java.lang.InterruptedException: sleep interrupted
73+
74+ */
75+
76+
77+
78+ /* yield()
79+ ThreadClassMethods t1 = new ThreadClassMethods();
80+ ThreadClassMethods t2 = new ThreadClassMethods();
81+
82+ t1.start();
83+ t2.start();
84+ */
85+
86+
87+
88+ ThreadClassMethods myThread = new ThreadClassMethods ();
89+ myThread .setDaemon (true );
90+ ThreadClassMethods t1 = new ThreadClassMethods ();
91+ t1 .start (); // now JVM will wait for t1, since that's user thread
92+ myThread .start ();
93+ System .out .println ("Main done" );
94+
95+ // main thread's work is completed, but JVM is waiting for the myThread to get completed
96+ // hence by setting that thread as daemon, now that is background threa, JVM will not wait for that and will exit
97+ }
98+ }
99+
100+ // start, run, sleep, join, setPriority, interrupt, yield, setDaemon
101+
102+ /*
103+ setPriority -> not strict, will just give hint to JVM and OS Scheduler
104+ interrupt -> if the thread is waiting/ sleeping, interrupt it
105+ yield -> A hint to the scheduler that the current thread is willing to yield its current use of a processor.
106+ The scheduler is free to ignore this hint.
107+
108+ USER THREADS -> which are actually doing work
109+ DAEMON THREADS -> which runs in background, like Garbage collector
110+ JVM will not wait for these threads
111+ */
0 commit comments