File tree Expand file tree Collapse file tree 15 files changed +428
-0
lines changed
src/main/java/com/chen/api/util/thread/study/chapter3 Expand file tree Collapse file tree 15 files changed +428
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .join_interrupt ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-15 01:34
6+ */
7+ public class Test {
8+
9+
10+ public static void main (String [] args ) {
11+ try {
12+ ThreadB threadB = new ThreadB ();
13+ threadB .start ();
14+ Thread .sleep (10 );
15+ ThreadC threadC = new ThreadC (threadB );
16+ threadC .start ();
17+ } catch (Exception e ) {
18+ e .printStackTrace ();
19+ }
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .join_interrupt ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-15 01:28
6+ */
7+ public class ThreadA extends Thread {
8+
9+ @ Override
10+ public void run () {
11+ for (int i = 0 ; i < 10000 ; i ++) {
12+ String a = new String ("a" );
13+ System .out .println ("ThreadA test....." );
14+ }
15+
16+ }
17+
18+
19+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .join_interrupt ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-15 01:28
6+ */
7+ public class ThreadB extends Thread {
8+
9+
10+ @ Override
11+ public void run () {
12+
13+ try {
14+ ThreadA threadA = new ThreadA ();
15+ threadA .start ();
16+ threadA .join ();
17+ System .out .println ("threadB 在end处打印了。。" );
18+ } catch (InterruptedException e ) {
19+ e .printStackTrace ();
20+ System .out .println ("threadB 在catch处打印了。。" );
21+
22+ }
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .join_interrupt ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-15 01:32
6+ */
7+ public class ThreadC extends Thread {
8+
9+ private ThreadB threadB ;
10+
11+ public ThreadC (ThreadB threadB ){
12+ this .threadB =threadB ;
13+ }
14+
15+
16+ @ Override
17+ public void run (){
18+ threadB .interrupt ();
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .join_test ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-15 01:04
6+ */
7+ public class Run {
8+
9+
10+ public static void main (String [] args ) {
11+
12+ Runnable runnable = new Runnable () {
13+ @ Override
14+ public void run () {
15+ try {
16+ int secondValue = (int )(Math .random () * 10000 );
17+ System .out .println (secondValue );
18+
19+ Thread .sleep (secondValue );
20+ } catch (InterruptedException e ) {
21+ e .printStackTrace ();
22+ }
23+ }
24+ };
25+
26+ Thread thread = new Thread (runnable );
27+ thread .start ();
28+ System .out .println ("我想在thread方法执行完毕后执行,但是sleep多少秒呢?" );
29+ System .out .println ("答案是不能确定!!" );
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .join_test ;
2+
3+ /**
4+ *
5+ * 方法join的作用是使所属的线程对象X正常执行run方法中的任务,而使当前线程Z进行无限制的阻塞,等待线程X销毁后在急需执行线程Z后的代码。
6+ *
7+ *
8+ * @author : chen weijie
9+ * @Date: 2018-05-15 01:04
10+ */
11+ public class Run2 {
12+
13+
14+ public static void main (String [] args ) {
15+
16+ Runnable runnable = new Runnable () {
17+ @ Override
18+ public void run () {
19+ try {
20+ int secondValue = (int ) (Math .random () * 10000 );
21+ System .out .println (secondValue );
22+
23+ Thread .sleep (secondValue );
24+ } catch (InterruptedException e ) {
25+ e .printStackTrace ();
26+ }
27+ }
28+ };
29+
30+ try {
31+ Thread thread = new Thread (runnable );
32+ thread .start ();
33+ thread .join ();
34+ System .out .println ("我想等待thread执行完毕才执行" );
35+ } catch (InterruptedException e ) {
36+ e .printStackTrace ();
37+ }
38+ }
39+
40+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .pipeInputOutput ;
2+
3+ import java .io .IOException ;
4+ import java .io .PipedInputStream ;
5+
6+ /**
7+ * @author : chen weijie
8+ * @Date: 2018-05-15 00:03
9+ */
10+ public class ReadData {
11+
12+ public void readMethod (PipedInputStream inputStream ) {
13+
14+ try {
15+ System .out .println ("read:" );
16+ byte [] bytes = new byte [20 ];
17+ int readLength = inputStream .read (bytes );
18+ while (readLength != -1 ) {
19+ String newData = new String (bytes , 0 , readLength );
20+
21+ System .out .println (newData );
22+ readLength = inputStream .read (bytes );
23+
24+ System .out .println ();
25+ inputStream .close ();
26+ }
27+ } catch (IOException e ) {
28+ e .printStackTrace ();
29+ }
30+ }
31+
32+
33+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .pipeInputOutput ;
2+
3+ import java .io .PipedInputStream ;
4+ import java .io .PipedOutputStream ;
5+
6+ /**
7+ * @author : chen weijie
8+ * @Date: 2018-05-15 00:13
9+ */
10+ public class Run {
11+
12+
13+ public static void main (String [] args ) {
14+
15+ ReadData readData = new ReadData ();
16+ PipedInputStream inputStream = new PipedInputStream ();
17+ WriteData writeData = new WriteData ();
18+ PipedOutputStream outputStream = new PipedOutputStream ();
19+ try {
20+ outputStream .connect (inputStream );
21+
22+ ThreadRead threadRead = new ThreadRead (readData , inputStream );
23+ threadRead .start ();
24+ Thread .sleep (2000 );
25+
26+ ThreadWrite threadWrite = new ThreadWrite (writeData , outputStream );
27+ threadWrite .start ();
28+ } catch (Exception e ) {
29+ e .printStackTrace ();
30+ }
31+
32+
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .pipeInputOutput ;
2+
3+ import java .io .PipedInputStream ;
4+
5+ /**
6+ * @author : chen weijie
7+ * @Date: 2018-05-15 00:12
8+ */
9+ public class ThreadRead extends Thread {
10+
11+
12+ private ReadData readdata ;
13+
14+ private PipedInputStream pipedInputStream ;
15+
16+ public ThreadRead (ReadData readdata , PipedInputStream pipedInputStream ) {
17+ this .readdata = readdata ;
18+ this .pipedInputStream = pipedInputStream ;
19+ }
20+
21+ public void run () {
22+ readdata .readMethod (pipedInputStream );
23+ }
24+
25+
26+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .pipeInputOutput ;
2+
3+ import java .io .PipedOutputStream ;
4+
5+ /**
6+ * @author : chen weijie
7+ * @Date: 2018-05-15 00:07
8+ */
9+ public class ThreadWrite extends Thread {
10+
11+
12+ private WriteData writeData ;
13+
14+ private PipedOutputStream outputStream ;
15+
16+ public ThreadWrite (WriteData writeData , PipedOutputStream outputStream ) {
17+ this .writeData = writeData ;
18+ this .outputStream = outputStream ;
19+ }
20+
21+
22+ @ Override
23+ public void run () {
24+ writeData .writeMethod (outputStream );
25+ }
26+
27+
28+ }
You can’t perform that action at this time.
0 commit comments