File tree Expand file tree Collapse file tree 13 files changed +400
-0
lines changed
java-multithread/src/main/java/com/brianway/learning/java/multithread/communication Expand file tree Collapse file tree 13 files changed +400
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+ public class MyThread extends Thread {
7
+ private Object lock ;
8
+
9
+ public MyThread (Object lock ) {
10
+ super ();
11
+ this .lock = lock ;
12
+ }
13
+
14
+ @ Override
15
+ public void run () {
16
+ Service service = new Service ();
17
+ service .testMethod (lock );
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+ public class NotifyThread extends Thread {
7
+ private Object lock ;
8
+
9
+ public NotifyThread (Object lock ) {
10
+ super ();
11
+ this .lock = lock ;
12
+ }
13
+
14
+ @ Override
15
+ public void run () {
16
+ synchronized (lock ){
17
+ lock .notify ();
18
+ }
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+ public class NotifyThread2 extends Thread {
7
+ private Object lock ;
8
+
9
+ public NotifyThread2 (Object lock ) {
10
+ super ();
11
+ this .lock = lock ;
12
+ }
13
+
14
+ @ Override
15
+ public void run () {
16
+ synchronized (lock ){
17
+ lock .notify ();
18
+ lock .notify ();
19
+ lock .notify ();
20
+ lock .notify ();
21
+
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+ public class NotifyThread3 extends Thread {
7
+ private Object lock ;
8
+
9
+ public NotifyThread3 (Object lock ) {
10
+ super ();
11
+ this .lock = lock ;
12
+ }
13
+
14
+ @ Override
15
+ public void run () {
16
+ synchronized (lock ){
17
+ lock .notifyAll ();
18
+ }
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+
7
+ /**
8
+ * P150
9
+ * 唤醒多个线程,(逆序?试了好多次,全部都是刚好逆序的。巧合?机制?)
10
+ */
11
+ public class Run3_notifyAll {
12
+ public static void main (String [] args ) throws InterruptedException {
13
+ Object lock = new Object ();
14
+ MyThread a = new MyThread (lock );
15
+ a .start ();
16
+ MyThread b = new MyThread (lock );
17
+ b .start ();
18
+ MyThread c = new MyThread (lock );
19
+ c .start ();
20
+
21
+ Thread .sleep (1000 );
22
+
23
+ NotifyThread3 notifyThread = new NotifyThread3 (lock );
24
+ notifyThread .start ();
25
+ }
26
+ }
27
+
28
+ /*
29
+ 输出:
30
+ begin wait(),ThreadName=Thread-0
31
+ begin wait(),ThreadName=Thread-1
32
+ begin wait(),ThreadName=Thread-2
33
+ end wait(),ThreadName=Thread-2
34
+ end wait(),ThreadName=Thread-1
35
+ end wait(),ThreadName=Thread-0
36
+
37
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+
7
+ /**
8
+ * P150
9
+ * 通知多个线程
10
+ */
11
+ public class Run3_notifyMany {
12
+ public static void main (String [] args ) throws InterruptedException {
13
+ Object lock = new Object ();
14
+ MyThread a = new MyThread (lock );
15
+ a .start ();
16
+ MyThread b = new MyThread (lock );
17
+ b .start ();
18
+ MyThread c = new MyThread (lock );
19
+ c .start ();
20
+
21
+ Thread .sleep (1000 );
22
+
23
+ NotifyThread2 notifyThread = new NotifyThread2 (lock );
24
+ notifyThread .start ();
25
+ }
26
+ }
27
+
28
+ /*
29
+ 输出:
30
+ begin wait(),ThreadName=Thread-0
31
+ begin wait(),ThreadName=Thread-2
32
+ begin wait(),ThreadName=Thread-1
33
+ end wait(),ThreadName=Thread-0
34
+ end wait(),ThreadName=Thread-1
35
+ end wait(),ThreadName=Thread-2
36
+
37
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+
7
+ /**
8
+ * P149
9
+ * 通知一个线程
10
+ */
11
+ public class Run3_notifyOne {
12
+ public static void main (String [] args ) throws InterruptedException {
13
+ Object lock = new Object ();
14
+ MyThread a = new MyThread (lock );
15
+ a .start ();
16
+ MyThread b = new MyThread (lock );
17
+ b .start ();
18
+ MyThread c = new MyThread (lock );
19
+ c .start ();
20
+
21
+ Thread .sleep (1000 );
22
+
23
+ NotifyThread notifyThread = new NotifyThread (lock );
24
+ notifyThread .start ();
25
+ }
26
+ }
27
+
28
+ /*
29
+ 输出:
30
+ begin wait(),ThreadName=Thread-1
31
+ begin wait(),ThreadName=Thread-2
32
+ begin wait(),ThreadName=Thread-0
33
+ end wait(),ThreadName=Thread-1
34
+
35
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example3 ;
2
+
3
+ import java .awt .font .TextHitInfo ;
4
+
5
+ /**
6
+ * Created by brian on 2016/4/14.
7
+ */
8
+ public class Service {
9
+ public void testMethod (Object lock ){
10
+ try {
11
+ synchronized (lock ){
12
+ System .out .println ("begin wait(),ThreadName=" +Thread .currentThread ().getName ());
13
+ lock .wait ();
14
+ System .out .println ("end wait(),ThreadName=" +Thread .currentThread ().getName ());
15
+ }
16
+ } catch (InterruptedException e ) {
17
+ e .printStackTrace ();
18
+ }
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example4 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+
7
+ /**
8
+ * p151
9
+ * wait(long)使用,超时自动唤醒
10
+ */
11
+ public class Run4_waitHasParam {
12
+ static private Object lock = new Object ();
13
+ static private Runnable runnable1 =new Runnable () {
14
+ public void run () {
15
+ try {
16
+ synchronized (lock ){
17
+ System .out .println ("wait begin timer=" +System .currentTimeMillis ());
18
+ lock .wait (4000 );
19
+ System .out .println ("wait end timer=" +System .currentTimeMillis ());
20
+ }
21
+ } catch (InterruptedException e ) {
22
+ e .printStackTrace ();
23
+ }
24
+ }
25
+ };
26
+
27
+ public static void main (String [] args ) {
28
+ Thread t = new Thread (runnable1 );
29
+ t .start ();
30
+ }
31
+ }
32
+
33
+
34
+ /*
35
+ 输出:
36
+ wait begin timer=1460566545742
37
+ wait end timer=1460566549743
38
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .communication .example4 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/14.
5
+ */
6
+
7
+ /**
8
+ * p152
9
+ * wait(long)使用,时间限制内由其他线程唤醒
10
+ */
11
+ public class Run4_waitHasParam2 {
12
+ static private Object lock = new Object ();
13
+ static private Runnable runnable1 =new Runnable () {
14
+ public void run () {
15
+ try {
16
+ synchronized (lock ){
17
+ System .out .println ("wait begin timer=" +System .currentTimeMillis ());
18
+ lock .wait (5000 );
19
+ System .out .println ("wait end timer=" +System .currentTimeMillis ());
20
+ }
21
+ } catch (InterruptedException e ) {
22
+ e .printStackTrace ();
23
+ }
24
+ }
25
+ };
26
+
27
+ static private Runnable runnable2 =new Runnable () {
28
+ public void run () {
29
+
30
+ synchronized (lock ){
31
+ System .out .println ("notify begin timer=" +System .currentTimeMillis ());
32
+ lock .notify ();
33
+ System .out .println ("notify end timer=" +System .currentTimeMillis ());
34
+ }
35
+
36
+ }
37
+ };
38
+
39
+
40
+ public static void main (String [] args ) throws InterruptedException {
41
+ Thread t1 = new Thread (runnable1 );
42
+ t1 .start ();
43
+ Thread .sleep (2000 );
44
+ Thread t2 = new Thread (runnable2 );
45
+ t2 .start ();
46
+ }
47
+ }
48
+
49
+
50
+ /*
51
+ 输出:
52
+ wait begin timer=1460566507815
53
+ notify begin timer=1460566509815
54
+ notify end timer=1460566509815
55
+ wait end timer=1460566509815
56
+ */
You can’t perform that action at this time.
0 commit comments