Skip to content

Commit 45ef97b

Browse files
committed
feat: 增加热点代码
1 parent 6ccf78a commit 45ef97b

1 file changed

Lines changed: 74 additions & 63 deletions

File tree

  • tool-java-hotcode/src/main/java/com/wdbyte/hotcode

tool-java-hotcode/src/main/java/com/wdbyte/hotcode/HotCode.java

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,43 @@ public class HotCode {
2121
public static void main(String[] args) {
2222
// 模拟 CPU 过高
2323
cpuHigh();
24-
// 模拟线程阻塞,线程池容量为1,塞入两个线程,会有一个一直等待
25-
thread();
24+
// 生成大长度数组
25+
allocate();
2626
// 模拟线程死锁
2727
deadThread();
2828
// 不断的向 hashSet 集合增加数据,内存缓慢增长
2929
addHashSetThread();
30-
// 生成大长度数组
31-
allocate();
30+
// 模拟线程阻塞,线程池容量为1,塞入两个线程,会有一个一直等待
31+
thread();
3232
}
3333

34+
/**
35+
* 消耗CPU的线程
36+
* 不断循环进行浮点运算
37+
*/
38+
private static void cpuHigh() {
39+
Thread thread = new Thread(() -> {
40+
Thread.currentThread().setName("cpu_high_thread");
41+
while (true){
42+
double pi = 0;
43+
for (int i = 0; i < Integer.MAX_VALUE; i++) {
44+
pi += Math.pow(-1, i) / (2 * i + 1);
45+
}
46+
System.out.println("Pi: " + pi * 4);
47+
}
48+
});
49+
thread.start();
50+
}
3451
private static Object array;
3552

3653
/**
3754
* 生成大长度数组
3855
*/
3956
private static void allocate() {
4057
new Thread(() -> {
58+
Thread.currentThread().setName("memory_allocate_thread_1");
4159
int index = 1;
4260
while (true) {
43-
Thread.currentThread().setName("memory_allocate_thread");
4461
array = new int[1 * index * 1000];
4562
array = new Integer[1 * index * 1000];
4663
try {
@@ -54,6 +71,7 @@ private static void allocate() {
5471
}).start();
5572

5673
new Thread(()->{
74+
Thread.currentThread().setName("memory_allocate_thread_2");
5775
List<String> list = new ArrayList<>();
5876
for (int i = 0; i < 1000000; i++) {
5977
try {
@@ -66,64 +84,6 @@ private static void allocate() {
6684
}).start();
6785
}
6886

69-
/**
70-
* 不断的向 hashSet 集合添加数据,每秒100个字符串
71-
*/
72-
public static void addHashSetThread() {
73-
// 初始化常量
74-
new Thread(() -> {
75-
int count = 0;
76-
while (true) {
77-
Thread.currentThread().setName("add_hash_set_thread");
78-
try {
79-
hashSet.add("count" + count);
80-
Thread.sleep(10);
81-
count++;
82-
} catch (InterruptedException e) {
83-
e.printStackTrace();
84-
}
85-
}
86-
}).start();
87-
}
88-
89-
/**
90-
* 极度消耗CPU的线程
91-
* 死循环
92-
*/
93-
private static void cpuHigh() {
94-
Thread thread = new Thread(() -> {
95-
Thread.currentThread().setName("cpu_high_thread");
96-
while (true){
97-
double pi = 0;
98-
for (int i = 0; i < Integer.MAX_VALUE; i++) {
99-
pi += Math.pow(-1, i) / (2 * i + 1);
100-
}
101-
System.out.println("Pi: " + pi * 4);
102-
}
103-
});
104-
thread.start();
105-
}
106-
107-
/**
108-
* 模拟线程阻塞
109-
* 线程池容量为1,但是向线程池中塞入两个线程
110-
*/
111-
private static void thread() {
112-
Thread thread = new Thread(() -> {
113-
while (true) {
114-
System.out.println("executorService thread start");
115-
try {
116-
Thread.sleep(1000);
117-
} catch (InterruptedException e) {
118-
e.printStackTrace();
119-
}
120-
}
121-
});
122-
// 添加到线程
123-
executorService.submit(thread);
124-
executorService.submit(thread);
125-
}
126-
12787
/**
12888
* 死锁线程
12989
* 线程 dead_thread_A 与 线程 dead_thread_B 互相锁死
@@ -167,4 +127,55 @@ private static void deadThread() {
167127
threadA.start();
168128
threadB.start();
169129
}
130+
131+
132+
/**
133+
* 不断的向 hashSet 集合添加数据,每秒100个字符串
134+
*/
135+
public static void addHashSetThread() {
136+
// 初始化常量
137+
new Thread(() -> {
138+
Thread.currentThread().setName("add_hash_set_thread");
139+
int count = 0;
140+
while (true) {
141+
try {
142+
hashSet.add("count" + count);
143+
Thread.sleep(10);
144+
count++;
145+
} catch (InterruptedException e) {
146+
e.printStackTrace();
147+
}
148+
}
149+
}).start();
150+
}
151+
152+
/**
153+
* 模拟线程阻塞
154+
* 线程池容量为1,但是向线程池中塞入两个线程
155+
*/
156+
private static void thread() {
157+
Thread thread = new Thread(() -> {
158+
System.out.println("executorService thread start");
159+
while (true) {
160+
try {
161+
Thread.sleep(1000);
162+
} catch (InterruptedException e) {
163+
e.printStackTrace();
164+
}
165+
}
166+
});
167+
// 添加到线程
168+
executorService.submit(thread);
169+
executorService.submit(thread);
170+
executorService.submit(thread);
171+
executorService.submit(thread);
172+
}
173+
174+
/**
175+
* 运行缓慢的方法
176+
*/
177+
public static void runSlowThread(){
178+
new Thread(() -> {
179+
}).start();
180+
}
170181
}

0 commit comments

Comments
 (0)