File tree Expand file tree Collapse file tree 10 files changed +238
-0
lines changed
main/java/cn/byhieg/threadtutorial/char02
test/java/cn/byhieg/threadtutorialtest/char02test Expand file tree Collapse file tree 10 files changed +238
-0
lines changed Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ /**
4+ * Created by byhieg on 17/1/1.
5+ * Mail to byhieg@gmail.com
6+ */
7+ public class HasSelfPrivateNum {
8+
9+ private int num = 0 ;
10+
11+ synchronized public void addI (String username ){
12+ try {
13+
14+ if (username .equals ("a" )){
15+ num = 100 ;
16+ System .out .println ("a set over!" );
17+ Thread .sleep (2000 );
18+ }else {
19+ num = 200 ;
20+ System .out .println ("b set over!" );
21+ }
22+ System .out .println (username + " num=" + num );
23+ }catch (Exception e ){
24+ e .printStackTrace ();
25+ }
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ import com .sun .xml .internal .ws .api .pipe .SyncStartForAsyncFeature ;
4+
5+ /**
6+ * Created by byhieg on 17/1/1.
7+ * Mail to byhieg@gmail.com
8+ */
9+ public class MyObject {
10+
11+ synchronized public void methodA (){
12+ try {
13+ System .out .println ("begin methodA threadName=" + Thread .currentThread ().getName ());
14+ Thread .sleep (5000 );
15+ System .out .println ("end" );
16+ }catch (InterruptedException e ){
17+ e .printStackTrace ();
18+ }
19+ }
20+
21+ synchronized public void methodB (){
22+ try {
23+ System .out .println ("begin methodB threadName=" + Thread .currentThread ().getName () +
24+ " begin time =" + System .currentTimeMillis ());
25+ Thread .sleep (5000 );
26+ System .out .println ("end" );
27+ }catch (InterruptedException e ){
28+ e .printStackTrace ();
29+ }
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ /**
4+ * Created by byhieg on 17/1/1.
5+ * Mail to byhieg@gmail.com
6+ */
7+ public class PublicVar {
8+
9+ public String username = "A" ;
10+ public String password = "AA" ;
11+
12+ synchronized public void setValue (String username ,String password ){
13+ try {
14+ this .username = username ;
15+ Thread .sleep (3000 );
16+ this .password = password ;
17+ System .out .println ("setValue method thread name=" + Thread .currentThread ().getName () + " username="
18+ + username + " password=" + password );
19+ }catch (InterruptedException e ){
20+ e .printStackTrace ();
21+ }
22+ }
23+
24+ synchronized public void getValue (){
25+ System .out .println ("getValue method thread name=" + Thread .currentThread ().getName () + " username=" + username
26+ + " password=" + password );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ /**
4+ * Created by byhieg on 17/1/1.
5+ * Mail to byhieg@gmail.com
6+ */
7+ public class PublicVarThreadA extends Thread {
8+
9+ private PublicVar publicVar ;
10+ public PublicVarThreadA (PublicVar publicVar ){
11+ this .publicVar = publicVar ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+ super .run ();
17+ publicVar .setValue ("B" ,"BB" );
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ /**
4+ * Created by byhieg on 17/1/1.
5+ * Mail to byhieg@gmail.com
6+ */
7+ public class SelfPrivateThreadA extends Thread {
8+ private HasSelfPrivateNum num ;
9+
10+
11+ public SelfPrivateThreadA (HasSelfPrivateNum num ){
12+ this .num = num ;
13+ }
14+ @ Override
15+ public void run () {
16+ super .run ();
17+ num .addI ("a" );
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ /**
4+ * Created by byhieg on 17/1/1.
5+ * Mail to byhieg@gmail.com
6+ */
7+ public class SelfPrivateThreadB extends Thread {
8+ private HasSelfPrivateNum num ;
9+
10+
11+ public SelfPrivateThreadB (HasSelfPrivateNum num ){
12+ this .num = num ;
13+ }
14+ @ Override
15+ public void run () {
16+ super .run ();
17+ num .addI ("b" );
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorial .char02 ;
2+
3+ import cn .byhieg .threadtutorial .char01 .SynchronizedObject ;
4+
5+ /**
6+ * Created by byhieg on 17/1/1.
7+ * Mail to byhieg@gmail.com
8+ */
9+ public class SynchronizedMethodThread extends Thread {
10+
11+ private MyObject object ;
12+
13+ public SynchronizedMethodThread (MyObject object ){
14+ this .object = object ;
15+ }
16+
17+ @ Override
18+ public void run () {
19+ super .run ();
20+ if (Thread .currentThread ().getName ().equals ("A" )){
21+ object .methodA ();
22+ }else {
23+ object .methodB ();
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char02test ;
2+
3+ import cn .byhieg .threadtutorial .char02 .HasSelfPrivateNum ;
4+ import cn .byhieg .threadtutorial .char02 .SelfPrivateThreadA ;
5+ import cn .byhieg .threadtutorial .char02 .SelfPrivateThreadB ;
6+ import junit .framework .TestCase ;
7+
8+ /**
9+ * Created by byhieg on 17/1/1.
10+ * Mail to byhieg@gmail.com
11+ */
12+ public class HasSelfPrivateNumTest extends TestCase {
13+ public void testAddI () throws Exception {
14+ HasSelfPrivateNum numA = new HasSelfPrivateNum ();
15+ HasSelfPrivateNum numB = new HasSelfPrivateNum ();
16+ SelfPrivateThreadA threadA = new SelfPrivateThreadA (numA );
17+ threadA .start ();
18+ SelfPrivateThreadB threadB = new SelfPrivateThreadB (numA );
19+ threadB .start ();
20+
21+ Thread .sleep (1000 * 3 );
22+ }
23+
24+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char02test ;
2+
3+ import cn .byhieg .threadtutorial .char02 .PublicVar ;
4+ import cn .byhieg .threadtutorial .char02 .PublicVarThreadA ;
5+ import junit .framework .TestCase ;
6+
7+ /**
8+ * Created by byhieg on 17/1/1.
9+ * Mail to byhieg@gmail.com
10+ */
11+ public class PublicVarThreadATest extends TestCase {
12+ public void testRun () throws Exception {
13+ PublicVar publicVarRef = new PublicVar ();
14+ PublicVarThreadA threadA = new PublicVarThreadA (publicVarRef );
15+ threadA .start ();
16+ Thread .sleep (40 );
17+ publicVarRef .getValue ();
18+ }
19+
20+ }
Original file line number Diff line number Diff line change 1+ package cn .byhieg .threadtutorialtest .char02test ;
2+
3+ import cn .byhieg .threadtutorial .char02 .MyObject ;
4+ import cn .byhieg .threadtutorial .char02 .SynchronizedMethodThread ;
5+ import junit .framework .TestCase ;
6+
7+ /**
8+ * Created by byhieg on 17/1/1.
9+ * Mail to byhieg@gmail.com
10+ */
11+ public class SynchronizedMethodThreadTest extends TestCase {
12+ public void testRun () throws Exception {
13+ MyObject object = new MyObject ();
14+ SynchronizedMethodThread a = new SynchronizedMethodThread (object );
15+ a .setName ("A" );
16+ SynchronizedMethodThread b = new SynchronizedMethodThread (object );
17+ b .setName ("B" );
18+
19+ a .start ();
20+ b .start ();
21+
22+ Thread .sleep (1000 * 15 );
23+ }
24+
25+ }
You can’t perform that action at this time.
0 commit comments