File tree Expand file tree Collapse file tree 17 files changed +392
-1
lines changed
src/main/java/com/chen/api/util/thread/study/chapter7 Expand file tree Collapse file tree 17 files changed +392
-1
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .groupInnerStop ;
2+
3+
4+ /**
5+ * @author : chen weijie
6+ * @Date: 2018-05-30 00:47
7+ */
8+ public class MyThread extends Thread {
9+
10+
11+ public MyThread (ThreadGroup group , String name ) {
12+ super (group , name );
13+ }
14+
15+ @ Override
16+ public void run () {
17+
18+ System .out .println ("threadName:" + Thread .currentThread ().getName () + "准备开始循环了" );
19+ while (!Thread .currentThread ().isInterrupted ()) {
20+
21+ }
22+
23+ System .out .println ("threadName:" + Thread .currentThread ().getName () + "结束循环了。" );
24+
25+ }
26+
27+
28+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .groupInnerStop ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-30 00:50
6+ */
7+ public class Run {
8+
9+ public static void main (String [] args ) {
10+ ThreadGroup threadGroup = new ThreadGroup ("我的线程组" );
11+
12+ try {
13+ for (int i = 0 ; i < 10 ; i ++) {
14+ MyThread thread = new MyThread (threadGroup , "线层:" + i );
15+ thread .start ();
16+ }
17+ Thread .sleep (5000 );
18+ threadGroup .interrupt ();
19+ System .out .println ("调用了interrupt方法。" );
20+ } catch (InterruptedException e ) {
21+ e .printStackTrace ();
22+ }
23+
24+ }
25+
26+
27+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-30 00:01
6+ */
7+ public class MyService {
8+
9+ synchronized public static void serviceMethod () {
10+ try {
11+ System .out .println ("线程:" + Thread .currentThread ().getName () + "调用serviceMethod方法" );
12+ Thread .sleep (10000 );
13+ } catch (InterruptedException e ) {
14+ e .printStackTrace ();
15+ }
16+ }
17+
18+
19+
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest2 ;
2+
3+
4+ /**
5+ * @author : chen weijie
6+ * @Date: 2018-05-30 00:03
7+ */
8+ public class Run {
9+
10+ public static void main (String [] args ) {
11+
12+ Thread1 thread1 = new Thread1 ();
13+ thread1 .setName ("1" );
14+ thread1 .start ();
15+
16+ Thread2 thread2 = new Thread2 ();
17+ thread2 .setName ("2" );
18+ thread2 .start ();
19+
20+ System .out .println ("thread2的状态:" + thread2 .getState ());
21+
22+
23+ }
24+
25+
26+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-30 00:11
6+ */
7+ public class Thread1 extends Thread {
8+
9+
10+
11+ @ Override
12+ public void run (){
13+
14+ MyService .serviceMethod ();
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-30 00:11
6+ */
7+ public class Thread2 extends Thread {
8+
9+
10+
11+ @ Override
12+ public void run (){
13+
14+ MyService .serviceMethod ();
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-29 23:43
6+ */
7+ public class MyThread extends Thread {
8+
9+
10+ @ Override
11+ public void run () {
12+ try {
13+ System .out .println ("begin sleep" );
14+ Thread .sleep (10000 );
15+ System .out .println ("end sleep" );
16+ } catch (InterruptedException e ) {
17+ e .printStackTrace ();
18+ }
19+
20+ }
21+
22+
23+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-29 23:45
6+ */
7+ public class Run {
8+
9+ public static void main (String [] args ) {
10+
11+ try {
12+ MyThread thread = new MyThread ();
13+ thread .start ();
14+ Thread .sleep (1000 );
15+ System .out .println ("thread state:" + thread .getState ());
16+ } catch (InterruptedException e ) {
17+ e .printStackTrace ();
18+ }
19+
20+
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest4 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-30 00:18
6+ */
7+ public class Lock {
8+
9+ public static final Byte lock = new Byte ("0" );
10+
11+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter7 .stateTest4 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-30 00:19
6+ */
7+ public class MyThread extends Thread {
8+
9+ @ Override
10+ public void run () {
11+
12+ synchronized (Lock .lock ) {
13+ try {
14+ Lock .lock .wait ();
15+ } catch (InterruptedException e ) {
16+ e .printStackTrace ();
17+ }
18+ }
19+ }
20+
21+
22+ }
You can’t perform that action at this time.
0 commit comments