File tree Expand file tree Collapse file tree 10 files changed +375
-0
lines changed
java-multithread/src/main/java/com/brianway/learning/java/multithread/communication Expand file tree Collapse file tree 10 files changed +375
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example1 ;
2+
3+
4+ import java .util .ArrayList ;
5+ import java .util .List ;
6+
7+ /**
8+ * Created by Brian on 2016/4/13.
9+ */
10+ public class MyList {
11+ private List list = new ArrayList ();
12+
13+ public void add (){
14+ list .add ("brian" );
15+ }
16+ public int size (){
17+ return list .size ();
18+ }
19+ }
20+
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example1 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+
7+ /**
8+ * P134
9+ * 不使用等待/通知机制实现线程间通信
10+ *
11+ * 这里有可见性的问题,并不一定能看到线程b退出。
12+ */
13+ public class Run1_TwoThreadTransData {
14+ public static void main (String [] args ) {
15+ MyList service = new MyList ();
16+ ThreadA a =new ThreadA (service );
17+ a .setName ("A" );
18+ a .start ();
19+ ThreadB b = new ThreadB (service );
20+ b .setName ("B" );
21+ b .start ();
22+ }
23+
24+ }
25+
26+
27+ /*
28+ 在ThreadB的run方法while里加上synchronized ("any"){}
29+
30+ 输出:
31+ 添加了1元素
32+ 添加了2元素
33+ 添加了3元素
34+ 添加了4元素
35+ java.lang.InterruptedException
36+ at com.brianway.learning.java.multithread.communication.example1.ThreadB.run(ThreadB.java:22)
37+ 添加了5元素
38+ 达到size了,线程b要退出了
39+ 添加了6元素
40+ 添加了7元素
41+ 添加了8元素
42+ 添加了9元素
43+ 添加了10元素
44+
45+
46+ ----------------
47+ 若不加synchronized ("any"){}
48+
49+ 输出:
50+ 添加了1元素
51+ 添加了2元素
52+ 添加了3元素
53+ 添加了4元素
54+ 添加了5元素
55+ 添加了6元素
56+ 添加了7元素
57+ 添加了8元素
58+ 添加了9元素
59+ 添加了10元素
60+ */
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example1 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+ public class ThreadA extends Thread {
7+ private MyList list ;
8+
9+ public ThreadA (MyList list ) {
10+ super ();
11+ this .list = list ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+ for (int i = 0 ;i <10 ;i ++){
17+ list .add ();
18+ System .out .println ("添加了" +(i +1 )+"元素" );
19+ try {
20+ Thread .sleep (1000 );
21+ } catch (InterruptedException e ) {
22+ e .printStackTrace ();
23+ }
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example1 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+ public class ThreadB extends Thread {
7+ private MyList list ;
8+
9+ public ThreadB (MyList list ) {
10+ super ();
11+ this .list = list ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+ try {
17+ while (true ){
18+ //System.out.println("in b while: "+list.size());
19+ //synchronized ("any"){}
20+ if (list .size () == 5 ){
21+ System .out .println ("达到size了,线程b要退出了" );
22+ throw new InterruptedException ();
23+ }
24+ }
25+ } catch (InterruptedException e ) {
26+ e .printStackTrace ();
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example2 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+
7+ /**
8+ * P137
9+ * 没有“对象监视器”,调用wait()方法出现运行时异常
10+ */
11+ public class Run2_noObjectLock {
12+ public static void main (String [] args ) {
13+ String s = new String ("" );
14+ try {
15+ s .wait ();
16+ } catch (InterruptedException e ) {
17+ e .printStackTrace ();
18+ }
19+ }
20+ }
21+
22+ /*
23+ 输出:
24+ Exception in thread "main" java.lang.IllegalMonitorStateException
25+ at java.lang.Object.wait(Native Method)
26+ at java.lang.Object.wait(Object.java:502)
27+ at com.brianway.learning.java.multithread.communication.example2.Run2_noObjectLock.main(Run2_noObjectLock.java:15)
28+ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
29+ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
30+ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
31+ at java.lang.reflect.Method.invoke(Method.java:483)
32+ at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
33+
34+ */
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example2 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+
7+ /**
8+ * P139
9+ * notify实现通知
10+ * notify调用后,并不会立即释放对象锁,而是退出synchronized代码块后
11+ */
12+ public class Run2_notify {
13+ public static void main (String [] args ) {
14+ try {
15+ Object lock = new Object ();
16+ Thread1 t1 = new Thread1 (lock );
17+ t1 .start ();
18+ Thread .sleep (3000 );
19+ Thread2 t2 = new Thread2 (lock );
20+ t2 .start ();
21+ } catch (InterruptedException e ) {
22+ e .printStackTrace ();
23+ }
24+
25+ }
26+ }
27+
28+
29+ /*
30+ 输出:
31+ 开始 wait time=1460554213416
32+ 开始 notify time=1460554216418
33+ 结束 notify time=1460554218418
34+ 结束 wait time=1460554218418
35+
36+ */
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example2 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+
7+ /**
8+ * P142
9+ * 测试sleep()期间其他线程是否执行
10+ * 处理器资源,对象锁,是不同的概念
11+ *
12+ */
13+ public class Run2_sleep {
14+ public static void main (String [] args ) {
15+ Thread t1 =new Thread (){
16+ @ Override
17+ public void run () {
18+ super .run ();
19+ System .out .println (Thread .currentThread ().getName ()+" begin sleep" );
20+ try {
21+ Thread .sleep (2000 );
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+ System .out .println (Thread .currentThread ().getName ()+" end sleep" );
26+ }
27+ };
28+ t1 .setName ("t1" );
29+
30+ Thread t2 = new Thread (){
31+ @ Override
32+ public void run () {
33+ super .run ();
34+ long i =0 ;
35+ long max = (long )Integer .MAX_VALUE ;
36+ System .out .println ("max = " +max );
37+
38+ System .out .println (Thread .currentThread ().getName ()+" begin while" );
39+ while (i <max ){
40+ i ++;
41+ if (i %100000000 ==0 ){
42+ System .out .println (Thread .currentThread ().getName ()+" now is " +i );
43+ }
44+ }
45+ System .out .println (Thread .currentThread ().getName ()+" end while" );
46+ }
47+ };
48+ t2 .setName ("t2" );
49+
50+ t1 .start ();
51+ t2 .start ();
52+
53+ }
54+ }
55+
56+ /*
57+ 输出:
58+ t1 begin sleep
59+ max = 2147483647
60+ t2 begin while
61+ t2 now is 100000000
62+ t2 now is 200000000
63+ t2 now is 300000000
64+ t2 now is 400000000
65+ t2 now is 500000000
66+ t2 now is 600000000
67+ t2 now is 700000000
68+ t2 now is 800000000
69+ t2 now is 900000000
70+ t2 now is 1000000000
71+ t2 now is 1100000000
72+ t2 now is 1200000000
73+ t2 now is 1300000000
74+ t2 now is 1400000000
75+ t2 now is 1500000000
76+ t1 end sleep
77+ t2 now is 1600000000
78+ t2 now is 1700000000
79+ t2 now is 1800000000
80+ t2 now is 1900000000
81+ t2 now is 2000000000
82+ t2 now is 2100000000
83+ t2 end while
84+ */
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example2 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+
7+ /**
8+ * P137
9+ * wait()永远阻塞
10+ */
11+ public class Run2_wait {
12+ public static void main (String [] args ) {
13+ try {
14+ String lock = new String ();
15+ System .out .println ("syn上面" );
16+ synchronized (lock ){
17+ System .out .println ("syn第一行,wait前面" );
18+ lock .wait ();
19+ System .out .println ("wait下面的代码" );
20+ }
21+ System .out .println ("syn下面的代码" );
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+ }
26+ }
27+
28+ /*
29+ 输出:
30+ syn上面
31+ syn第一行,wait前面
32+ */
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example2 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+ public class Thread1 extends Thread {
7+ private Object lock ;
8+
9+ public Thread1 (Object lock ) {
10+ super ();
11+ this .lock = lock ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+ try {
17+ synchronized (lock ){
18+ System .out .println ("开始 wait time=" +System .currentTimeMillis ());
19+ lock .wait ();
20+ System .out .println ("结束 wait time=" +System .currentTimeMillis ());
21+ }
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .multithread .communication .example2 ;
2+
3+ /**
4+ * Created by Brian on 2016/4/13.
5+ */
6+ public class Thread2 extends Thread {
7+ private Object lock ;
8+
9+ public Thread2 (Object lock ) {
10+ super ();
11+ this .lock = lock ;
12+ }
13+
14+ @ Override
15+ public void run () {
16+
17+ synchronized (lock ){
18+ System .out .println ("开始 notify time=" +System .currentTimeMillis ());
19+ lock .notify ();
20+ try {
21+ Thread .sleep (2000 );
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+ System .out .println ("结束 notify time=" +System .currentTimeMillis ());
26+ }
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments