-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh13_76_ThreadPriorities.java
More file actions
60 lines (51 loc) · 2.25 KB
/
Ch13_76_ThreadPriorities.java
File metadata and controls
60 lines (51 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Intercom extends Thread{
public Intercom(String name){
super(name);
}
public void run(){
System.out.println("Person On the Door - " + this.getName());
}
}
public class Ch13_76_ThreadPriorities {
public static void main(String[] args) {
System.out.println();
/* In JVM, upon making multiple threads a queue is made on which of the threads will be executed in which order
Ex. T1 > T2 > T3 so on...
Now on our end , we can set the priorities of these thread-
Note: Min_Priority = 01
Normal_Priority = 05
Max_Priority = 10
Syntax -
After making object -
object.setPriority(int Value)
1. For max priority - Thread.MAX_PRIORITY
2. For min priority - Thread.MIN_PRIORITY
3. For normal priority - Thread.NORM_PRIORITY */
Intercom person1 = new Intercom("Brother 1");
Intercom person2 = new Intercom("Brother 2");
Intercom person3 = new Intercom("Brother 3");
person1.start();
person2.start();
person3.start();
// You will notice different threads getting executed at random order due to difference in their priority
System.out.println("*******");
// Now setting priorities
System.out.println("Priorities set in Intercom-");
Intercom person4 = new Intercom("Father - Max Priority");
Intercom person5 = new Intercom("Mother - Max Priority");
Intercom person6 = new Intercom("Delivery Boy");
Intercom person7 = new Intercom("Sister - Min Priority");
person4.setPriority(Thread.MAX_PRIORITY);
person7.setPriority(Thread.MIN_PRIORITY);
person5.setPriority(Thread.MAX_PRIORITY);
person5.setPriority(6);
person4.start();
person5.start();
person6.start();
person7.start();
// You will notice max - priority threads will be executed above than others at most of the time
// And Min Priority will be almost at the end.
/* ** Note depending on different situations of CPU these priority threads are executed.
Ex. If CPU load is high you will notice min priorities being executed. */
}
}