-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestee.java
More file actions
101 lines (87 loc) · 2.96 KB
/
Testee.java
File metadata and controls
101 lines (87 loc) · 2.96 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
package com.oldratlee.jvisualvm;
import java.util.Date;
/**
* @author Jerry Lee
*/
public class Testee {
static long countTimeIn500ms = 0;
public static void main(String[] args) throws Exception {
countTimeIn500ms = guessCountTimeIn500ms();
Thread runAndSleepTask = new Thread(new RunAndSleepTask(), "RunAndSleepTask");
Thread runAndWaitTask = new Thread(new RunAndWaitTask(), "RunAndWaitTask");
Thread runAndLongSleepTask = new Thread(new RunAndLongSleepTask(), "RunAndLongSleepTask");
runAndSleepTask.start();
runAndWaitTask.start();
runAndLongSleepTask.start();
System.out.println("Tasks started!");
Thread.sleep(Long.MAX_VALUE);
}
public static class RunAndSleepTask implements Runnable {
@Override
public void run() {
String name = this.getClass().getSimpleName();
while (true) {
for (long i = 0; i < countTimeIn500ms; i++) {
}
System.out.println(name + ": " + new Date());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static class RunAndWaitTask implements Runnable {
@Override
public void run() {
String name = this.getClass().getSimpleName();
while (true) {
for (long i = 0; i < countTimeIn500ms; i++) {
}
try {
System.out.println(name + ": " + new Date());
synchronized (this) {
this.wait(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static class RunAndLongSleepTask implements Runnable {
@Override
public void run() {
String name = this.getClass().getSimpleName();
while (true) {
for (long i = 0; i < countTimeIn500ms; i++) {
}
System.out.println(name + ": " + new Date());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* @return 100ms可以做{@code long}的计数次数。
*/
static long guessCountTimeIn500ms() {
System.out.println("start guess.");
long count = 1000L * 1000 * 1000;
// simple warm-up
for (long i = 0; i < count; i++) {
// nothing!
}
long tick = System.currentTimeMillis();
for (long i = 0; i < count; i++) {
// nothing!
}
long duration = System.currentTimeMillis() - tick;
System.out.printf("1G times count take %d ms.\n", duration);
return count * 500 / duration;
}
}