Skip to content

Commit 17af0f4

Browse files
committed
feat: 增加热点代码
1 parent 3619192 commit 17af0f4

3 files changed

Lines changed: 184 additions & 0 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>tool-java-object-pool</module>
1717
<module>tool-java-jackson</module>
1818
<module>tool-java-apache-common</module>
19+
<module>tool-java-hotcode</module>
1920
</modules>
2021
<name>parent-modules</name>
2122
<description>Parent for all java modules</description>

tool-java-hotcode/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<parent>
4+
<artifactId>parent-modules</artifactId>
5+
<groupId>com.wdbyte</groupId>
6+
<version>1.0.0-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<artifactId>tool-java-hotcode</artifactId>
10+
11+
12+
<properties>
13+
<maven.compiler.source>17</maven.compiler.source>
14+
<maven.compiler.target>17</maven.compiler.target>
15+
</properties>
16+
</project>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package com.wdbyte.hotcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
9+
/**
10+
* @author niulang
11+
* @date 2023/02/20
12+
*/
13+
public class HotCode {
14+
15+
private static HashSet hashSet = new HashSet();
16+
/**
17+
* 线程池,大小1
18+
*/
19+
private static ExecutorService executorService = Executors.newFixedThreadPool(1);
20+
21+
public static void main(String[] args) {
22+
// 模拟 CPU 过高
23+
//cpuHigh();
24+
// 模拟线程阻塞,线程池容量为1,塞入两个线程,会有一个一直等待
25+
thread();
26+
// 模拟线程死锁
27+
deadThread();
28+
// 不断的向 hashSet 集合增加数据,内存缓慢增长
29+
addHashSetThread();
30+
// 生成大长度数组
31+
allocate();
32+
}
33+
34+
private static Object array;
35+
36+
/**
37+
* 生成大长度数组
38+
*/
39+
private static void allocate() {
40+
new Thread(() -> {
41+
int index = 1;
42+
while (true) {
43+
Thread.currentThread().setName("memory_allocate_thread");
44+
array = new int[1 * index * 1000];
45+
array = new Integer[1 * index * 1000];
46+
try {
47+
Thread.sleep(1000);
48+
} catch (InterruptedException e) {
49+
throw new RuntimeException(e);
50+
}
51+
index++;
52+
53+
}
54+
}).start();
55+
56+
new Thread(()->{
57+
List<String> list = new ArrayList<>();
58+
for (int i = 0; i < 1000000; i++) {
59+
try {
60+
Thread.sleep(1);
61+
} catch (InterruptedException e) {
62+
throw new RuntimeException(e);
63+
}
64+
list.add("string" + i);
65+
}
66+
}).start();
67+
}
68+
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+
double pi = 0;
96+
for (int i = 0; i < Integer.MAX_VALUE; i++) {
97+
pi += Math.pow(-1, i) / (2 * i + 1);
98+
}
99+
System.out.println("Pi: " + pi * 4);
100+
});
101+
thread.start();
102+
}
103+
104+
/**
105+
* 模拟线程阻塞
106+
* 线程池容量为1,但是向线程池中塞入两个线程
107+
*/
108+
private static void thread() {
109+
Thread thread = new Thread(() -> {
110+
while (true) {
111+
System.out.println("executorService thread start");
112+
try {
113+
Thread.sleep(1000);
114+
} catch (InterruptedException e) {
115+
e.printStackTrace();
116+
}
117+
}
118+
});
119+
// 添加到线程
120+
executorService.submit(thread);
121+
executorService.submit(thread);
122+
}
123+
124+
/**
125+
* 死锁线程
126+
* 线程 dead_thread_A 与 线程 dead_thread_B 互相锁死
127+
*/
128+
private static void deadThread() {
129+
/** 创建资源 */
130+
Object resourceA = new Object();
131+
Object resourceB = new Object();
132+
// 创建线程
133+
Thread threadA = new Thread(() -> {
134+
Thread.currentThread().setName("dead_thread_A");
135+
synchronized (resourceA) {
136+
System.out.println(Thread.currentThread() + " get ResourceA");
137+
try {
138+
Thread.sleep(1000);
139+
} catch (InterruptedException e) {
140+
e.printStackTrace();
141+
}
142+
System.out.println(Thread.currentThread() + "waiting get resourceB");
143+
synchronized (resourceB) {
144+
System.out.println(Thread.currentThread() + " get resourceB");
145+
}
146+
}
147+
});
148+
149+
Thread threadB = new Thread(() -> {
150+
Thread.currentThread().setName("dead_thread_A");
151+
synchronized (resourceB) {
152+
System.out.println(Thread.currentThread() + " get ResourceB");
153+
try {
154+
Thread.sleep(1000);
155+
} catch (InterruptedException e) {
156+
e.printStackTrace();
157+
}
158+
System.out.println(Thread.currentThread() + "waiting get resourceA");
159+
synchronized (resourceA) {
160+
System.out.println(Thread.currentThread() + " get resourceA");
161+
}
162+
}
163+
});
164+
threadA.start();
165+
threadB.start();
166+
}
167+
}

0 commit comments

Comments
 (0)