-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathThreadPriorites.java
More file actions
36 lines (29 loc) · 1.4 KB
/
ThreadPriorites.java
File metadata and controls
36 lines (29 loc) · 1.4 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
/* 4. Run the following program and learn the concept of thread priority. */
class myThread extends Thread{
public void run() {
System.out.println("Inside run method");
}
}
public class ThreadPriorites {
public static void main(String[] args) {
myThread t1 = new myThread();
myThread t2 = new myThread();
myThread t3 = new myThread();
System.out.println("t1 thread priority : " + t1.getPriority()); // Default 5
System.out.println("t2 thread priority : " + t2.getPriority()); // Default 5
System.out.println("t3 thread priority : " + t3.getPriority()); // Default 5
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
// t3.setPriority(21); will throw IllegalArgumentException
System.out.println("t1 thread priority : " + t1.getPriority()); //2
System.out.println("t2 thread priority : " + t2.getPriority()); //5
System.out.println("t3 thread priority : " + t3.getPriority());//8
// Main thread
System.out.print(Thread.currentThread().getName());
System.out.println("Main thread priority : "+ Thread.currentThread().getPriority());
// Main thread priority is set to 10
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : "+ Thread.currentThread().getPriority());
}
}