Repository navigation
Expand file tree
/
Copy pathRun18_priority02.java
More file actions
123 lines (107 loc) · 3.5 KB
/
Copy pathRun18_priority02.java
File metadata and controls
123 lines (107 loc) · 3.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.brianway.learning.java.multithread.meet;
/**
* Created by Brian on 2016/4/11.
*/
import java.util.Random;
/**
* P45例子
* 优先级具有规则性
*/
class MyThread18_1 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult += i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★ thread1 use time = " + (endTime - beginTime));
}
}
class MyThread18_2 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult += i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆ thread2 use time = " + (endTime - beginTime));
}
}
public class Run18_priority02 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread18_1 myThread18_1 = new MyThread18_1();
myThread18_1.setPriority(10);
//myThread18_1.setPriority(1);
myThread18_1.start();
MyThread18_2 myThread18_2 = new MyThread18_2();
//myThread18_2.setPriority(10);
myThread18_2.setPriority(1);
myThread18_2.start();
}
}
}
/*
myThread18_1.setPriority(10);
myThread18_2.setPriority(1);
输出:
☆☆☆☆☆ thread2 use time = 202
★★★★★ thread1 use time = 208
★★★★★ thread1 use time = 255
★★★★★ thread1 use time = 261
★★★★★ thread1 use time = 268
★★★★★ thread1 use time = 270
☆☆☆☆☆ thread2 use time = 321
☆☆☆☆☆ thread2 use time = 338
☆☆☆☆☆ thread2 use time = 344
☆☆☆☆☆ thread2 use time = 35
-------------------------
★★★★★ thread1 use time = 121
★★★★★ thread1 use time = 232
☆☆☆☆☆ thread2 use time = 268
☆☆☆☆☆ thread2 use time = 292
★★★★★ thread1 use time = 322
☆☆☆☆☆ thread2 use time = 325
★★★★★ thread1 use time = 387
☆☆☆☆☆ thread2 use time = 390
★★★★★ thread1 use time = 395
☆☆☆☆☆ thread2 use time = 409
----------------------------------------------------
----------------------------------------------------
myThread18_1.setPriority(1);
myThread18_2.setPriority(10);
输出:
☆☆☆☆☆ thread2 use time = 393
☆☆☆☆☆ thread2 use time = 455
☆☆☆☆☆ thread2 use time = 496
☆☆☆☆☆ thread2 use time = 509
☆☆☆☆☆ thread2 use time = 510
★★★★★ thread1 use time = 511
★★★★★ thread1 use time = 554
★★★★★ thread1 use time = 558
★★★★★ thread1 use time = 561
★★★★★ thread1 use time = 569
---------------
☆☆☆☆☆ thread2 use time = 214
☆☆☆☆☆ thread2 use time = 230
☆☆☆☆☆ thread2 use time = 237
★★★★★ thread1 use time = 257
☆☆☆☆☆ thread2 use time = 261
☆☆☆☆☆ thread2 use time = 271
★★★★★ thread1 use time = 350
★★★★★ thread1 use time = 369
★★★★★ thread1 use time = 339
★★★★★ thread1 use time = 382
*/