Browse Source

test线程安全 try

yegang 2 years ago
parent
commit
465fcfee83
1 changed files with 30 additions and 0 deletions
  1. 30 0
      src/test/java/com/sw/propagation/ConTest.java

+ 30 - 0
src/test/java/com/sw/propagation/ConTest.java

@@ -56,4 +56,34 @@ public class ConTest {
         }).start();
 
     }
+    @Test
+    public void tryLock() {
+        ReentrantLock reentrantLock = new ReentrantLock();
+        new Thread(() -> {
+            reentrantLock.lock();
+            try {
+                System.out.println("异常前");
+                int a = 1 / 0;
+                System.out.println("异常后");
+            }catch (Exception e){
+                e.printStackTrace();
+            }finally {
+                reentrantLock.unlock();
+            }
+        }).start();
+        new Thread(() -> {
+            reentrantLock.lock();
+            try {
+                System.out.println("异常前");
+                int a = 1 / 0;
+                System.out.println("异常后");
+            }catch (Exception e){
+                e.printStackTrace();
+            }finally {
+                reentrantLock.unlock();
+            }
+        }).start();
+
+
+    }
 }