File tree Expand file tree Collapse file tree 3 files changed +93
-0
lines changed
spring-boot-student-concurrent/src/main/java/com/xiaolyuh Expand file tree Collapse file tree 3 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .xiaolyuh ;
2+
3+ import java .time .LocalDateTime ;
4+ import java .time .format .DateTimeFormatter ;
5+
6+ /**
7+ * 守护线程
8+ *
9+ * @author yuhao.wang3
10+ */
11+ public class DaemonThread {
12+
13+ public static void main (String [] args ) throws InterruptedException {
14+ Thread thread = new Thread (() -> {
15+ try {
16+ while (!Thread .currentThread ().isInterrupted ()) {
17+ System .out .println (Thread .currentThread ().getName ()
18+ + " 任务执行 " + LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss_SSS" )));
19+ Thread .sleep (50 );
20+ }
21+ System .out .println (Thread .currentThread ().getName ()
22+ + " 任务中断 " + LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss_SSS" )));
23+ } catch (InterruptedException e ) {
24+ e .printStackTrace ();
25+ Thread .currentThread ().interrupt ();
26+ } finally {
27+ System .out .println ("...........finally" );
28+ }
29+ });
30+ thread .setDaemon (true );
31+ thread .start ();
32+ Thread .sleep (500 );
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package com .xiaolyuh ;
2+
3+ import java .time .LocalDateTime ;
4+ import java .time .format .DateTimeFormatter ;
5+
6+ /**
7+ * 中断线程有异常
8+ *
9+ * @author yuhao.wang3
10+ */
11+ public class HasInterrputException {
12+ public static void main (String [] args ) throws InterruptedException {
13+ Thread thread = new Thread (() -> {
14+ String threadName = Thread .currentThread ().getName ();
15+ while (!Thread .currentThread ().isInterrupted ()) {
16+ try {
17+ System .out .println ("thread:" + LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss_SSS" )));
18+ Thread .sleep (3000 );
19+ } catch (InterruptedException e ) {
20+ System .out .println (threadName + " catch interrput flag is " + Thread .currentThread ().isInterrupted () +
21+ " at " + (LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss_SSS" ))));
22+ Thread .currentThread ().interrupt ();
23+ e .printStackTrace ();
24+ }
25+ System .out .println (threadName );
26+ }
27+ System .out .println (threadName + " interrput flag is " + Thread .currentThread ().isInterrupted ());
28+ });
29+ thread .start ();
30+ System .out .println ("Main:" + LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss_SSS" )));
31+ Thread .sleep (800 );
32+ System .out .println ("Main begin interrupt thread:" + LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss_SSS" )));
33+ thread .interrupt ();
34+ }
35+
36+ }
Original file line number Diff line number Diff line change 1+ package com .xiaolyuh ;
2+
3+ import java .lang .management .ManagementFactory ;
4+ import java .lang .management .ThreadInfo ;
5+ import java .lang .management .ThreadMXBean ;
6+
7+ /**
8+ * @author yuhao.wang
9+ */
10+ public class MainThread {
11+
12+ public static void main (String [] args ) {
13+ //虚拟机线程管理的接口
14+ ThreadMXBean threadMXBean = ManagementFactory .getThreadMXBean ();
15+ ThreadInfo [] threadInfos =
16+ threadMXBean .dumpAllThreads (false , false );
17+
18+ for (ThreadInfo threadInfo :threadInfos ) {
19+ System .out .println ("[" +threadInfo .getThreadId ()+"]" +" "
20+ +threadInfo .getThreadName ());
21+ }
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments