File tree Expand file tree Collapse file tree 8 files changed +205
-0
lines changed
src/main/java/com/chen/api/util/thread/study/chapter2 Expand file tree Collapse file tree 8 files changed +205
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .atomicIntegerTest ;
2+
3+ import java .util .concurrent .atomic .AtomicInteger ;
4+
5+ /**
6+ * @author chen weijie
7+ * @date 2018-04-22 11:29 PM
8+ */
9+ public class AddCountThread extends Thread {
10+
11+ private AtomicInteger count = new AtomicInteger ();
12+
13+
14+ @ Override
15+ public void run () {
16+ for (int i = 0 ; i < 100 ; i ++) {
17+ System .out .println (count .incrementAndGet ());
18+ }
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .atomicIntegerTest ;
2+
3+ /**
4+ * @author chen weijie
5+ * @date 2018-04-22 11:32 PM
6+ */
7+ public class Run {
8+
9+
10+ public static void main (String [] args ) {
11+
12+ AddCountThread addCountThread = new AddCountThread ();
13+
14+ Thread thread1 = new Thread (addCountThread );
15+ thread1 .start ();
16+ Thread thread2 = new Thread (addCountThread );
17+ thread2 .start ();
18+ Thread thread3 = new Thread (addCountThread );
19+ thread3 .start ();
20+ Thread thread4 = new Thread (addCountThread );
21+ thread4 .start ();
22+
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .t100 ;
2+
3+ /**
4+ * @author chen weijie
5+ * @date 2018-04-22 8:07 PM
6+ */
7+ public class PrintString implements Runnable {
8+
9+
10+ private boolean isContinuePrint = true ;
11+
12+ public boolean isContinuePrint () {
13+ return isContinuePrint ;
14+ }
15+
16+ public void setContinuePrint (boolean continuePrint ) {
17+ isContinuePrint = continuePrint ;
18+ }
19+
20+
21+ @ Override
22+ public void run () {
23+
24+ int i = 0 ;
25+
26+ while (isContinuePrint ) {
27+ i ++;
28+ try {
29+ System .out .println ("run printStringMethod,thread =" + Thread .currentThread ().getName () + ",i==" + i );
30+ Thread .sleep (100 );
31+ } catch (InterruptedException e ) {
32+ e .printStackTrace ();
33+ }
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .t100 ;
2+
3+ /**
4+ * @author chen weijie
5+ * @date 2018-04-22 8:08 PM
6+ */
7+ public class Test {
8+
9+ public static void main (String [] args ) throws InterruptedException {
10+
11+ PrintString printString = new PrintString ();
12+ new Thread (printString ).start ();
13+ Thread .sleep (1000 );
14+ System .out .println ("想要停止它..." );
15+ printString .setContinuePrint (false );
16+
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .t99 ;
2+
3+ /**
4+ * 这种的情况是停不了的,while是死循环.
5+ *
6+ * @author chen weijie
7+ * @date 2018-04-22 7:58 PM
8+ */
9+ public class PrintString {
10+
11+ private boolean isContinuePrint = true ;
12+
13+ public boolean isContinuePrint () {
14+ return isContinuePrint ;
15+ }
16+
17+ public void setContinuePrint (boolean continuePrint ) {
18+ isContinuePrint = continuePrint ;
19+ }
20+
21+ public void printStringMethod () {
22+
23+ int i = 0 ;
24+
25+ while (isContinuePrint ) {
26+ i ++;
27+ try {
28+ System .out .println ("run printStringMethod,thread =" + Thread .currentThread ().getName () + ",i==" + i );
29+ Thread .sleep (100 );
30+ } catch (InterruptedException e ) {
31+ e .printStackTrace ();
32+ }
33+ }
34+
35+
36+ }
37+
38+
39+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .t99 ;
2+
3+ /**
4+ * @author chen weijie
5+ * @date 2018-04-22 8:02 PM
6+ */
7+ public class Test {
8+
9+ public static void main (String [] args ) {
10+ PrintString printString = new PrintString ();
11+ printString .printStringMethod ();
12+ System .out .println ("我要停止它" );
13+ printString .setContinuePrint (false );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .volatileTestThread ;
2+
3+ /**
4+ * @author chen weijie
5+ * @date 2018-04-22 10:58 PM
6+ */
7+ public class MyThread extends Thread {
8+
9+ volatile public static int count ;
10+
11+ private static void addCount () {
12+
13+ for (int i = 0 ; i < 100 ; i ++) {
14+ count ++;
15+ }
16+ System .out .println ("count==" + count );
17+ }
18+
19+ @ Override
20+ public void run () {
21+ addCount ();
22+ }
23+
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter2 .volatileTestThread ;
2+
3+ /**
4+ * 运行的结果不是10000
5+ *
6+ * @author chen weijie
7+ * @date 2018-04-22 11:01 PM
8+ */
9+ public class Run {
10+
11+ public static void main (String [] args ) {
12+
13+ MyThread [] myThreadArray = new MyThread [100 ];
14+
15+ for (int i = 0 ; i < 100 ; i ++) {
16+ myThreadArray [i ] = new MyThread ();
17+ }
18+
19+ for (int i = 0 ; i < 100 ; i ++) {
20+ myThreadArray [i ].start ();
21+ }
22+ }
23+
24+ }
You can’t perform that action at this time.
0 commit comments