|
@@ -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();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
}
|