File tree Expand file tree Collapse file tree 11 files changed +201
-0
lines changed
main/java/cn/byhieg/threadtutorial/char01
test/java/cn/byhieg/threadtutorialtest/char01test Expand file tree Collapse file tree 11 files changed +201
-0
lines changed Original file line number Diff line number Diff line change 1919 <version >3.8.1</version >
2020 <scope >test</scope >
2121 </dependency >
22+ <dependency >
23+ <groupId >junit</groupId >
24+ <artifactId >junit</artifactId >
25+ <version >RELEASE</version >
26+ <scope >test</scope >
27+ </dependency >
2228 </dependencies >
2329
2430
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char01 ;
2+
3+ /**
4+ * Created by shiqifeng on 2016/12/27.
5+ * Mail byhieg@gmail.com
6+ */
7+ public class ExampleCurrentThread extends Thread {
8+
9+ public ExampleCurrentThread (){
10+ System .out .println ("构造方法的打印:" + Thread .currentThread ().getName ());
11+ }
12+
13+ @ Override
14+ public void run () {
15+ super .run ();
16+ System .out .println ("run方法的打印:" + Thread .currentThread ().getName ());
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char01 ;
2+
3+ /**
4+ * Created by shiqifeng on 2016/12/27.
5+ * Mail byhieg@gmail.com
6+ */
7+ public class ExampleRunable implements Runnable {
8+
9+ public void run () {
10+ System .out .println ("这是实现Runnable接口的类" );
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char01 ;
2+
3+ /**
4+ * Created by shiqifeng on 2016/12/27.
5+ * Mail byhieg@gmail.com
6+ */
7+ public class ExampleThread extends Thread {
8+
9+ @ Override
10+ public void run () {
11+ super .run ();
12+ System .out .println ("这是一个继承自Thread的ExampleThread" );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char01 ;
2+
3+ /**
4+ * Created by shiqifeng on 2016/12/27.
5+ * Mail byhieg@gmail.com
6+ */
7+ public class NotShareDataThread extends Thread {
8+
9+ private int count = 5 ;
10+
11+
12+ public NotShareDataThread (String name ){
13+ super ();
14+ this .setName (name );
15+ }
16+
17+ @ Override
18+ public void run () {
19+ super .run ();
20+ System .out .println ("++++++++++++++++++" );
21+ while (count > 0 ){
22+ count --;
23+ System .out .println (currentThread ().getName () + "计算,count=" +count );
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char01 ;
2+
3+ /**
4+ * Created by shiqifeng on 2016/12/27.
5+ * Mail byhieg@gmail.com
6+ */
7+ public class ShareDataThread extends Thread {
8+
9+ private int count = 5 ;
10+
11+ @ Override
12+ public void run () {
13+ super .run ();
14+ count --;
15+ System .out .println (currentThread ().getName () + "count=" + count );
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char01test ;
2+
3+ import cn .byhieg .threadtutorial .char01 .ExampleCurrentThread ;
4+ import junit .framework .TestCase ;
5+
6+ /**
7+ * Created by shiqifeng on 2016/12/27.
8+ * Mail byhieg@gmail.com
9+ */
10+ public class ExampleCurrentThreadTest extends TestCase {
11+
12+ public void testInit () throws Exception {
13+ ExampleCurrentThread thread = new ExampleCurrentThread ();
14+ }
15+
16+ public void testRun () throws Exception {
17+ ExampleCurrentThread thread = new ExampleCurrentThread ();
18+ thread .start ();
19+
20+ Thread .sleep (1000 );
21+
22+ }
23+
24+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char01test ;
2+
3+ import cn .byhieg .threadtutorial .char01 .ExampleRunable ;
4+ import junit .framework .TestCase ;
5+
6+ /**
7+ * Created by shiqifeng on 2016/12/27.
8+ * Mail byhieg@gmail.com
9+ */
10+ public class ExampleRunableTest extends TestCase {
11+ public void testRun () throws Exception {
12+ new Thread (new ExampleRunable ()).start ();
13+
14+ Thread .sleep (1000 );
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char01test ;
2+
3+ import cn .byhieg .threadtutorial .char01 .ExampleThread ;
4+ import junit .framework .TestCase ;
5+
6+ /**
7+ * Created by shiqifeng on 2016/12/27.
8+ * Mail byhieg@gmail.com
9+ */
10+ public class ExampleThreadTest extends TestCase {
11+
12+ public void testRun () throws Exception {
13+ ExampleThread exampleThread = new ExampleThread ();
14+ exampleThread .start ();
15+
16+ Thread .sleep (1000 );
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char01test ;
2+
3+ import cn .byhieg .threadtutorial .char01 .NotShareDataThread ;
4+ import junit .framework .TestCase ;
5+
6+ /**
7+ * Created by shiqifeng on 2016/12/27.
8+ * Mail byhieg@gmail.com
9+ */
10+ public class NotShareDataThreadTest extends TestCase {
11+
12+ public void testRun () throws Exception {
13+ NotShareDataThread a = new NotShareDataThread ("A" );
14+ NotShareDataThread b = new NotShareDataThread ("B" );
15+ NotShareDataThread c = new NotShareDataThread ("C" );
16+
17+ a .start ();
18+ b .start ();
19+ c .start ();
20+
21+ Thread .sleep (1000 );
22+ }
23+
24+ }
You can’t perform that action at this time.
0 commit comments