From 2228819828168d3848ae50f535b1f4cbd9f3ea7c Mon Sep 17 00:00:00 2001 From: yuanwei Date: Sat, 4 Jun 2022 10:39:43 +0800 Subject: [PATCH] demo Code --- .../notify/wait/NotifyAllDemo.java" | 9 +- .../concurrent/notify/wait/NotifyDemo.java" | 1 - .../notify/wait/ResourceAAndB.java" | 59 +++ .../notify/wait/WaitNotifyInterrupt.java" | 18 +- .../code/pattern-all/.idea/workspace.xml" | 352 +++--------------- 5 files changed, 122 insertions(+), 317 deletions(-) create mode 100644 "\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/ResourceAAndB.java" rename "\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterupt.java" => "\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterrupt.java" (50%) diff --git "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyAllDemo.java" "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyAllDemo.java" index 5d0b4d2..be6e92f 100644 --- "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyAllDemo.java" +++ "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyAllDemo.java" @@ -6,11 +6,11 @@ package com.david.concurrent.notify.wait; * @date 2021/7/19 17:21 */ public class NotifyAllDemo { - private static final Object resource=new Object(); + private static final Object resource = new Object(); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { - synchronized (resource){ + synchronized (resource) { System.out.println("t1 get resource lock!"); try { System.out.println("t1 begin wait!"); @@ -22,7 +22,7 @@ public class NotifyAllDemo { } }); Thread t2 = new Thread(() -> { - synchronized (resource){ + synchronized (resource) { System.out.println("t2 get resource lock!"); try { System.out.println("t2 begin wait!"); @@ -33,9 +33,8 @@ public class NotifyAllDemo { } } }); - Thread t3 = new Thread(() -> { - synchronized (resource){ + synchronized (resource) { System.out.println("t3 begin notify"); resource.notifyAll(); } diff --git "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyDemo.java" "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyDemo.java" index ea9a850..64477f7 100644 --- "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyDemo.java" +++ "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/NotifyDemo.java" @@ -33,7 +33,6 @@ public class NotifyDemo { } } }); - Thread t3 = new Thread(() -> { synchronized (resource){ System.out.println("t3 begin notify"); diff --git "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/ResourceAAndB.java" "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/ResourceAAndB.java" new file mode 100644 index 0000000..491553c --- /dev/null +++ "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/ResourceAAndB.java" @@ -0,0 +1,59 @@ +package com.david.concurrent.notify.wait; + +/** + * wait方法只会释放当前共享变量对应线程的锁 + * @author Wade + * @version V1.0 + * @Package com.david.concurrent.notify.wait + * @date 2022/6/4 10:26 + */ +public class ResourceAAndB { + private static volatile Object resourceA = new Object(); + private static volatile Object resourceB = new Object(); + + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(() -> { + synchronized (resourceA) { + System.out.println("t1 获取到resourceA的监视锁"); + synchronized (resourceB) { + System.out.println("t1 获取到resourceB的监视锁"); + System.out.println("t1 释放resourceA的监视锁"); + try { + resourceA.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }); + + Thread t2 = new Thread(() -> { + try { + Thread.sleep(1000L); + synchronized (resourceA) { + System.out.println("t2 获取到resourceA的监视锁"); + System.out.println("t2 尝试获取resourceB的监视锁..."); + synchronized (resourceB) { + System.out.println("t2 获取到resourceB的监视锁"); + System.out.println("t2 释放resourceA的监视锁"); + resourceA.wait(); + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + t1.start(); + t2.start(); + + t1.join(); + t2.join(); + System.out.println("main method over!"); + } +} +//输出: +//t1 获取到resourceA的监视锁 +//t1 获取到resourceB的监视锁 +//t1 释放resourceA的监视锁 +//t2 获取到resourceA的监视锁 +//t2 尝试获取resourceB的监视锁... \ No newline at end of file diff --git "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterupt.java" "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterrupt.java" similarity index 50% rename from "\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterupt.java" rename to "\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterrupt.java" index 41f779c..b098d6d 100644 --- "a/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterupt.java" +++ "b/\345\271\266\345\217\221/code/concurrent-all/concurrent-base/src/main/java/com/david/concurrent/notify/wait/WaitNotifyInterrupt.java" @@ -2,17 +2,20 @@ package com.david.concurrent.notify.wait; /** + * 一个线程调用共享变量的wait方法被阻塞挂起后,如果其他线程中断了该线程会抛出InterruptedException异常 + * * @author wade * @version 1.0 * @date 2021/7/19 17:00 */ -public class WaitNotifyInterupt { - private final static Object lock=new Object(); +public class WaitNotifyInterrupt { + private final static Object lock = new Object(); + public static void main(String[] args) throws InterruptedException { Thread t = new Thread(() -> { try { System.out.println("---begin---"); - synchronized (lock){ + synchronized (lock) { lock.wait(); } System.out.println("---end---"); @@ -28,3 +31,12 @@ public class WaitNotifyInterupt { System.out.println("---begin interrupt t---"); } } +//输出: +//---begin--- +// ---begin interrupt t--- +// ---begin interrupt t--- +// java.lang.InterruptedException +// at java.lang.Object.wait(Native Method) +// at java.lang.Object.wait(Object.java:502) +// at com.david.concurrent.notify.wait.WaitNotifyInterrupt.lambda$main$0(WaitNotifyInterrupt.java:16) +// at java.lang.Thread.run(Thread.java:748) diff --git "a/\350\256\276\350\256\241\346\250\241\345\274\217/code/pattern-all/.idea/workspace.xml" "b/\350\256\276\350\256\241\346\250\241\345\274\217/code/pattern-all/.idea/workspace.xml" index ad02a61..d294eba 100644 --- "a/\350\256\276\350\256\241\346\250\241\345\274\217/code/pattern-all/.idea/workspace.xml" +++ "b/\350\256\276\350\256\241\346\250\241\345\274\217/code/pattern-all/.idea/workspace.xml" @@ -2,290 +2,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + @@ -302,12 +23,12 @@ - + - - + + @@ -321,8 +42,8 @@ - - + + @@ -331,7 +52,7 @@ - + @@ -339,8 +60,8 @@ - - + + @@ -348,8 +69,8 @@ - - + + @@ -443,7 +164,7 @@