-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPriority.java
More file actions
56 lines (43 loc) · 1.02 KB
/
Copy pathThreadPriority.java
File metadata and controls
56 lines (43 loc) · 1.02 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
/**
* 功能:理解线程调试中优先级
* 步骤:
*
*/
import java.util.*;
public class ThreadPriority {
public static void main(String[] args) {
//用Thread类的子类创建线程
InheritThread rtd = new InheritThread();
rtd.setPriority(1);
rtd.start();
//用Runnalbe接口类的对象创建线程
Runnable r = new RunnableThread();
Thread rt = new Thread(r);
rt.setPriority(10);
rt.start();
}
}
class InheritThread extends Thread {
public void run() {
System.out.println("InheritThread is running...");
for(int i=0; i<10; i++) {
System.out.println("InheritThread:i="+i);
try {
Thread.sleep((int)Math.random() * 1000);
} catch(InterruptedException e) {
}
}
}
}
class RunnableThread implements Runnable {
//自定义线程的run()方法
public void run() {
System.out.println("RunnableThread is running...");
for(int i=0; i<10; i++)
System.out.println("RunnalbeThread: i="+i);
try {
Thread.sleep((int)Math.random() * 1000);
} catch(InterruptedException e) {
}
}
}