Skip to content

Commit ddce472

Browse files
committed
update HotCode.java
1 parent bdc331c commit ddce472

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

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

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.wdbyte.hotcode;
22

3+
import java.io.IOException;
34
import java.math.BigDecimal;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
47
import java.util.ArrayList;
58
import java.util.HashSet;
69
import java.util.List;
@@ -9,7 +12,7 @@
912

1013
/**
1114
*
12-
* @author niulang
15+
* @author https://www.wdbyte.com
1316
* @date 2023/02/20
1417
*/
1518
public class HotCode {
@@ -33,6 +36,10 @@ public static void main(String[] args) {
3336
thread();
3437
// 运行缓慢的方法
3538
runSlowThread();
39+
// 读取文件
40+
readFile();
41+
// 抛出异常
42+
exceMethod();
3643
}
3744

3845
/**
@@ -104,6 +111,7 @@ private static void deadThread() {
104111
System.out.println(Thread.currentThread() + " get ResourceB");
105112
try {
106113
Thread.sleep(1000);
114+
107115
} catch (InterruptedException e) {
108116
e.printStackTrace();
109117
}
@@ -194,4 +202,45 @@ public static void slow2() throws InterruptedException {
194202
System.out.println(count);
195203
}
196204

205+
/**
206+
* 不断读取文件
207+
*/
208+
public static void readFile(){
209+
new Thread(() -> {
210+
Thread.currentThread().setName("read_file_method");
211+
while (true){
212+
try {
213+
byte[] bytes = Files.readAllBytes(Paths.get("/Users/darcy/Downloads/info.txt"));
214+
System.out.println(bytes.length);
215+
Thread.sleep(100);
216+
} catch (IOException e) {
217+
throw new RuntimeException(e);
218+
} catch (InterruptedException e) {
219+
throw new RuntimeException(e);
220+
}
221+
}
222+
}).start();
223+
}
224+
225+
/**
226+
* 不断抛出异常
227+
*/
228+
public static void exceMethod() {
229+
new Thread(() -> {
230+
Thread.currentThread().setName("exce_method");
231+
while (true) {
232+
try {
233+
System.out.println(exce(0));
234+
Thread.sleep(200);
235+
} catch (Exception e) {
236+
e.printStackTrace();
237+
}
238+
}
239+
}).start();
240+
}
241+
242+
public static int exce(int a){
243+
return 10/a;
244+
}
245+
197246
}

0 commit comments

Comments
 (0)