-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadEx2.java
More file actions
60 lines (49 loc) · 1.35 KB
/
ThreadEx2.java
File metadata and controls
60 lines (49 loc) · 1.35 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
//Thread priority and sleep!
//Thread class internally implements Runnable Interface
//Runnable Interface has the method 'run()'
//our class -> extends -> Thread class -> implements -> Runnable Interface!
class A extends Thread{ //this is a thread!
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hi");
try {
Thread.sleep(10); //waiting stage! in milli seconds
}
//sleeping thread sometimes waken up by another thread!
//this causes exception!
catch (InterruptedException e) { e.printStackTrace();}
}
}
}
class B extends Thread{
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hello");
try {
Thread.sleep(10);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
public class ThreadEx2{
public static void main(String args[]){
A objA = new A();
B objB = new B();
//check priority of thread
//priority range(1-10)
//least to highest
//5 is default(normal priority)!
System.out.println(objA.getPriority());
System.out.println(objB.getPriority());
//pass a suggestion to scheduler!
//MIN_PRIORITY,MAX_PRIORITY_PRIORITY,NORM_PRIORITY
objA.setPriority(Thread.MAX_PRIORITY);
objB.setPriority(Thread.MAX_PRIORITY);
//start the threads
objA.start();
try {
Thread.sleep(2);
} catch (InterruptedException e) { e.printStackTrace();}
objB.start();
}
}