88import java .util .concurrent .Executors ;
99
1010/**
11+ *
1112 * @author niulang
1213 * @date 2023/02/20
1314 */
@@ -22,14 +23,16 @@ public class HotCode {
2223 public static void main (String [] args ) {
2324 // 模拟 CPU 过高
2425 cpuHigh ();
25- // 不断新增 BigDecimal 信息到 list
26+ // 不断新增 BigDecimal 信息到 list,每秒10000个,内存迅速上升
2627 allocate ();
2728 // 模拟线程死锁
2829 deadThread ();
2930 // 不断的向 hashSet 集合增加数据,内存缓慢增长
3031 addHashSetThread ();
3132 // 模拟线程阻塞,线程池容量为1,塞入两个线程,会有一个一直等待
3233 thread ();
34+ // 运行缓慢的方法
35+ runSlowThread ();
3336 }
3437
3538 /**
@@ -51,7 +54,7 @@ private static void cpuHigh() {
5154 }
5255
5356 /**
54- * 不断新增 BigDecimal 信息到 list
57+ * 不断新增 BigDecimal 信息到 list,每秒10000个
5558 */
5659 private static void allocate () {
5760 new Thread (()->{
@@ -63,7 +66,9 @@ private static void allocate() {
6366 } catch (InterruptedException e ) {
6467 throw new RuntimeException (e );
6568 }
66- list .add (new BigDecimal (i ));
69+ for (int i1 = 0 ; i1 < 10 ; i1 ++) {
70+ list .add (new BigDecimal (i ));
71+ }
6772 }
6873 }).start ();
6974 }
@@ -160,6 +165,33 @@ private static void thread() {
160165 */
161166 public static void runSlowThread (){
162167 new Thread (() -> {
168+ Thread .currentThread ().setName ("slow_method" );
169+ while (true ){
170+ try {
171+ slow ();
172+ slow2 ();
173+ } catch (InterruptedException e ) {
174+ throw new RuntimeException (e );
175+ }
176+ }
163177 }).start ();
164178 }
179+
180+ public static void slow () throws InterruptedException {
181+ int count = 0 ;
182+ for (int i = 0 ; i < 10000 ; i ++) {
183+ count ++;
184+ Thread .sleep (1 );
185+ }
186+ System .out .println (count );
187+ }
188+ public static void slow2 () throws InterruptedException {
189+ int count = 0 ;
190+ for (int i = 0 ; i < 1000 ; i ++) {
191+ count ++;
192+ Thread .sleep (1 );
193+ }
194+ System .out .println (count );
195+ }
196+
165197}
0 commit comments