Explorar o código

test线程安全 try

yegang %!s(int64=2) %!d(string=hai) anos
pai
achega
465fcfee83
Modificáronse 1 ficheiros con 30 adicións e 0 borrados
  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();
+
+
+    }
 }