/*** @author: ChenforCode* @date: 2019年4月10日 22:59* @description: 在同步方法抛出异常的时候会立即释放锁*/public class SynchronizedException9 implements Runnable{private static SynchronizedException9 instance = new SynchronizedException9();public synchronized void method1(){System.out.println("线程进入,名字是:" + Thread.currentThread().getName());try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}throw new RuntimeException();//System.out.println(Thread.currentThread().getName() + "运行结束");}public synchronized void method2(){System.out.println("线程进入,名字是:" + Thread.currentThread().getName());try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "运行结束");}public static void main(String[] args){Thread t1 = new Thread(instance);Thread t2 = new Thread(instance);t1.start();t2.start();while (t1.isAlive() || t2.isAlive()){}System.out.println("finish");}@Overridepublic void run() {if (Thread.currentThread().getName().equals("Thread-0")){method1();} else {method2();}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。