Skip to content

Commit 080568b

Browse files
committed
feat: 增加热点代码
1 parent 4147125 commit 080568b

File tree

1 file changed

+6
-21
lines changed
  • tool-java-hotcode/src/main/java/com/wdbyte/hotcode

1 file changed

+6
-21
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class HotCode {
2222
public static void main(String[] args) {
2323
// 模拟 CPU 过高
2424
cpuHigh();
25-
// 生成大长度数组
25+
// 不断新增 BigDecimal 信息到 list
2626
allocate();
2727
// 模拟线程死锁
2828
deadThread();
@@ -49,36 +49,21 @@ private static void cpuHigh() {
4949
});
5050
thread.start();
5151
}
52-
private static Object array;
5352

5453
/**
55-
* 生成大长度数组
54+
* 不断新增 BigDecimal 信息到 list
5655
*/
5756
private static void allocate() {
58-
new Thread(() -> {
59-
Thread.currentThread().setName("memory_allocate_thread_1");
60-
int index = 1;
61-
while (true) {
62-
array = new BigDecimal[1 * index * 1000];
63-
try {
64-
Thread.sleep(1000);
65-
} catch (InterruptedException e) {
66-
throw new RuntimeException(e);
67-
}
68-
index++;
69-
}
70-
}).start();
71-
7257
new Thread(()->{
73-
Thread.currentThread().setName("memory_allocate_thread_2");
74-
List<String> list = new ArrayList<>();
75-
for (int i = 0; i < 1000000; i++) {
58+
Thread.currentThread().setName("memory_allocate_thread");
59+
List<BigDecimal> list = new ArrayList<>();
60+
for (int i = 0; i < Integer.MAX_VALUE; i++) {
7661
try {
7762
Thread.sleep(1);
7863
} catch (InterruptedException e) {
7964
throw new RuntimeException(e);
8065
}
81-
list.add("string" + i);
66+
list.add(new BigDecimal(i));
8267
}
8368
}).start();
8469
}

0 commit comments

Comments
 (0)