File tree 6 files changed +92
-1
lines changed
6 files changed +92
-1
lines changed Original file line number Diff line number Diff line change @@ -49,4 +49,5 @@ hs_err_pid*
49
49
target
50
50
.mvn
51
51
mvnw
52
- mvnw.cmd
52
+ mvnw.cmd
53
+ out
Original file line number Diff line number Diff line change
1
+ 本文件用于说明包用途:
2
+ - base: 创建线程的两种方式
Original file line number Diff line number Diff line change
1
+ public class Main {
2
+
3
+ public static void main (String [] args ) {
4
+ System .out .println ("Hello World!" );
5
+ System .out .println ("当前线程名为:" +Thread .currentThread ().getName ());
6
+ System .out .println ("当前线程id为:" +Thread .currentThread ().getId ());
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ package base ;
2
+
3
+ /**
4
+ * 类功能简述:
5
+ * 类功能详述:
6
+ *
7
+ * @author fanxb
8
+ * @date 2019/4/9 10:54
9
+ */
10
+ public class CustomExceptionHandler implements Thread .UncaughtExceptionHandler {
11
+
12
+ @ Override
13
+ public void uncaughtException (Thread t , Throwable e ) {
14
+ System .out .println ("捕获到线程" +t .getName ()+",异常:" + e .getMessage ());
15
+ e .printStackTrace ();
16
+ }
17
+
18
+ public static void main (String [] args ) {
19
+ Thread .setDefaultUncaughtExceptionHandler (new CustomExceptionHandler ());
20
+ new Thread (() -> {
21
+ throw new RuntimeException ("test" );
22
+ }).start ();
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package base ;
2
+
3
+ /**
4
+ * 类功能简述:
5
+ * 类功能详述:
6
+ *
7
+ * @author fanxb
8
+ * @date 2019/4/8 11:04
9
+ */
10
+ public class CustomThreadExtendThread extends Thread {
11
+
12
+ @ Override
13
+ public void run () {
14
+ String threadName = Thread .currentThread ().getName ();
15
+ long threadId = Thread .currentThread ().getId ();
16
+ System .out .println ("创建线程名为:" +threadName +",id为:" +threadId );
17
+ }
18
+
19
+ public static void main (String [] args ){
20
+ Thread thread1 = new CustomThreadExtendThread ();
21
+ Thread thread2 = new CustomThreadExtendThread ();
22
+ thread1 .start ();
23
+ thread2 .start ();
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package base ;
2
+
3
+ /**
4
+ * 类功能简述:
5
+ * 类功能详述:
6
+ *
7
+ * @author fanxb
8
+ * @date 2019/4/8 11:11
9
+ */
10
+ public class CustomThreadImplementInterface implements Runnable {
11
+
12
+ @ Override
13
+ public void run () {
14
+ Thread .currentThread ().setName (((Double ) Math .random ()).toString ());
15
+ String threadName = Thread .currentThread ().getName ();
16
+ long threadId = Thread .currentThread ().getId ();
17
+ System .out .println ("创建线程名为:" + threadName + ",id为:" + threadId );
18
+ }
19
+
20
+ public static void main (String [] args ) {
21
+ Thread thread1 = new Thread (new CustomThreadImplementInterface ());
22
+ Thread thread2 = new Thread (new CustomThreadExtendThread ());
23
+ thread1 .start ();
24
+ thread2 .start ();
25
+
26
+ //使用lambda表达式,让创建线程更简单
27
+ new Thread (() -> {
28
+ System .out .println ("创建了一个新线程" );
29
+ }).start ();
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments