File tree Expand file tree Collapse file tree
02nio/nio02/src/main/java/thread/github/pxd Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package thread .github .pxd ;
2+
3+ public class TestDemonThread {
4+
5+ public static void main (String [] args ) {
6+
7+ Runnable task = () -> {
8+
9+ try {
10+ Thread .sleep (5000 );
11+ } catch (InterruptedException e ) {
12+ e .printStackTrace ();
13+ }
14+
15+ Thread t = Thread .currentThread ();
16+ System .out .println ("当前线程:" + t .getName ());
17+
18+ };
19+
20+ Thread thread = new Thread (task );
21+ thread .setName ("test-thread-1" );
22+
23+ //这个地方如果是守护线程,那么进程里所有的线程都是守护线程,则会在进程结束的时候,会清理掉所有线程
24+ //thread.setDaemon(true);
25+ thread .start ();
26+
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package thread .github .pxd ;
2+
3+ public class ThreadJoinTest {
4+
5+ public static void main (String [] args ) {
6+
7+ Runnable task = () -> {
8+
9+ try {
10+ Thread .sleep (5000 );
11+ } catch (InterruptedException e ) {
12+ e .printStackTrace ();
13+ }
14+
15+ Thread t = Thread .currentThread ();
16+ System .out .println ("当前线程:" + t .getName ());
17+
18+ };
19+
20+ Thread thread1 = new Thread (task );
21+ thread1 .setName ("test-thread-1" );
22+
23+ Thread thread2 = new Thread (() -> {
24+ try {
25+ Thread t = Thread .currentThread ();
26+ System .out .println ("当前线程:" + t .getName ());
27+
28+ thread1 .join ();
29+
30+ } catch (InterruptedException e ) {
31+ e .printStackTrace ();
32+ }
33+
34+ });
35+ thread2 .setName ("test-thread-2" );
36+
37+ thread1 .start ();
38+ thread2 .start ();
39+ }
40+
41+ }
You can’t perform that action at this time.
0 commit comments