File tree Expand file tree Collapse file tree 9 files changed +224
-0
lines changed
java-multithread/src/main/java/com/brianway/learning/java/multithread/lock/example1 Expand file tree Collapse file tree 9 files changed +224
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/15.
5
+ */
6
+
7
+
8
+ /**
9
+ * P206
10
+ * 使用Condition实现等待通知
11
+ * Condition对象的await()方法,线程WAITING
12
+ */
13
+ public class Run1_UseConditionWaitNotify {
14
+ public static void main (String [] args ) {
15
+ ServiceB service = new ServiceB ();
16
+ ThreadB b = new ThreadB (service );
17
+ b .start ();
18
+ }
19
+ }
20
+
21
+
22
+ /*
23
+ 输出:
24
+ A
25
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/15.
5
+ */
6
+
7
+
8
+ /**
9
+ * P204
10
+ * 使用Condition实现等待通知,展示错误用法
11
+ * IllegalMonitorStateException,监视器出错
12
+ */
13
+ public class Run1_UseConditionWaitNotifyError {
14
+ public static void main (String [] args ) {
15
+ ServiceA service = new ServiceA ();
16
+ ThreadA a = new ThreadA (service );
17
+ a .start ();
18
+ }
19
+ }
20
+
21
+
22
+ /*
23
+ 输出:
24
+ Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
25
+ at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
26
+ at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
27
+ at java.util.concurrent.locks.AbstractQueuedSynchronizer.fullyRelease(AbstractQueuedSynchronizer.java:1723)
28
+ at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2036)
29
+ at com.brianway.learning.java.multithread.lock.example1.ServiceA.await(ServiceA.java:16)
30
+ at com.brianway.learning.java.multithread.lock.example1.ThreadA.run(ThreadA.java:15)
31
+
32
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/15.
5
+ */
6
+
7
+
8
+ /**
9
+ * P207
10
+ * 正确使用Condition实现等待通知
11
+ * Object类的wait()------Condition类的await()
12
+ * Object类的wait(long timeout)------Condition类的await(long time,TimeUnit unit)
13
+ * Object类的notify()------Condition类的signal()
14
+ * Object类的notifyAll()------Condition类的signalAll()
15
+ */
16
+ public class Run1_UseConditionWaitNotifyOk {
17
+ public static void main (String [] args ) throws InterruptedException {
18
+ ServiceC service = new ServiceC ();
19
+ ThreadC c = new ThreadC (service );
20
+ c .start ();
21
+ Thread .sleep (3000 );
22
+ service .signal ();
23
+ }
24
+ }
25
+
26
+
27
+ /*
28
+ 输出:
29
+ await 时间为1460651340418
30
+ signal时间为1460651343418
31
+ 锁释放了
32
+ */
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ import java .util .concurrent .locks .Condition ;
4
+ import java .util .concurrent .locks .Lock ;
5
+ import java .util .concurrent .locks .ReentrantLock ;
6
+
7
+ /**
8
+ * Created by brian on 2016/4/15.
9
+ */
10
+ public class ServiceA {
11
+ private Lock lock = new ReentrantLock ();
12
+ private Condition condition = lock .newCondition ();
13
+
14
+ public void await (){
15
+ try {
16
+ condition .await ();
17
+ } catch (InterruptedException e ) {
18
+ e .printStackTrace ();
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ import java .util .concurrent .locks .Condition ;
4
+ import java .util .concurrent .locks .Lock ;
5
+ import java .util .concurrent .locks .ReentrantLock ;
6
+
7
+ /**
8
+ * Created by brian on 2016/4/15.
9
+ */
10
+ public class ServiceB {
11
+ private Lock lock = new ReentrantLock ();
12
+ private Condition condition = lock .newCondition ();
13
+
14
+ public void waitMethod (){
15
+ try {
16
+ lock .lock ();
17
+ System .out .println ("A" );
18
+ condition .await ();
19
+ System .out .println ("B" );
20
+ } catch (InterruptedException e ) {
21
+ e .printStackTrace ();
22
+ }finally {
23
+ lock .unlock ();
24
+ System .out .println ("锁释放了" );
25
+ }
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ import java .util .concurrent .locks .Condition ;
4
+ import java .util .concurrent .locks .Lock ;
5
+ import java .util .concurrent .locks .ReentrantLock ;
6
+
7
+ /**
8
+ * Created by brian on 2016/4/15.
9
+ */
10
+ public class ServiceC {
11
+ private Lock lock = new ReentrantLock ();
12
+ private Condition condition = lock .newCondition ();
13
+
14
+ public void await (){
15
+ try {
16
+ lock .lock ();
17
+ System .out .println ("await 时间为" +System .currentTimeMillis ());
18
+ condition .await ();
19
+ } catch (InterruptedException e ) {
20
+ e .printStackTrace ();
21
+ }finally {
22
+ lock .unlock ();
23
+ System .out .println ("锁释放了" );
24
+ }
25
+ }
26
+
27
+ public void signal (){
28
+ try {
29
+ lock .lock ();
30
+ System .out .println ("signal时间为" +System .currentTimeMillis ());
31
+ condition .signal ();
32
+ } finally {
33
+ lock .unlock ();
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/15.
5
+ */
6
+ public class ThreadA extends Thread {
7
+ private ServiceA service ;
8
+
9
+ public ThreadA (ServiceA service ) {
10
+ this .service = service ;
11
+ }
12
+
13
+ @ Override
14
+ public void run () {
15
+ service .await ();
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/15.
5
+ */
6
+ public class ThreadB extends Thread {
7
+ private ServiceB service ;
8
+
9
+ public ThreadB (ServiceB service ) {
10
+ this .service = service ;
11
+ }
12
+
13
+ @ Override
14
+ public void run () {
15
+ service .waitMethod ();
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .java .multithread .lock .example1 ;
2
+
3
+ /**
4
+ * Created by brian on 2016/4/15.
5
+ */
6
+ public class ThreadC extends Thread {
7
+ private ServiceC service ;
8
+
9
+ public ThreadC (ServiceC service ) {
10
+ this .service = service ;
11
+ }
12
+
13
+ @ Override
14
+ public void run () {
15
+ service .await ();
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments