forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHighLowPriority.java
More file actions
53 lines (50 loc) · 1.32 KB
/
HighLowPriority.java
File metadata and controls
53 lines (50 loc) · 1.32 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
package JavaBasic;/**
* @Classname HighLowPriority
* @Description TODO
* @Date 19-5-25 下午1:46
* @Created by mao<tianmao818@qq.com>
*/
public class HighLowPriority {
public static void main(String[] args){
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker high=new Clicker(Thread.NORM_PRIORITY+2);
Clicker low=new Clicker(Thread.NORM_PRIORITY-2);
high.start();
low.start();
try {
Thread.sleep(1000);
}catch (InterruptedException e){
System.out.println("Main thread interrupted");
}
high.stop();
low.stop();
// try{
// high.t.join();
// low.t.join();
// }catch (InterruptedException e){
// System.out.println("Interrupt exception caught");
// }
System.out.println("low priority clicker:"+low.click);
System.out.println("high priority clicker:"+high.click);
}
}
class Clicker implements Runnable{
int click=0;
Thread t;
private volatile boolean running=true;
public Clicker(int p){
t=new Thread(this);
t.setPriority(p);
}
public void run(){
while (running){
click++;
}
}
public void stop(){
running=false;
}
public void start(){
t.start();
}
}