public class Demo { final static Lock lock = new ReentrantLock(); public static void test(Long userId) throws InterruptedException { System.out.println("------线程开始进入-----" + new Date()); lock.lock(); try { System.err.println("打印..." + new Date()); Thread.sleep(5000); } catch (Exception e) { }finally { lock.unlock(); //必须在finally中解锁 } } public static void test2(Long userId) throws InterruptedException { System.out.println("------线程开始进入-----" + new Date()); synchronized (String.valueOf(userId).intern()) { System.err.println("打印..." + new Date()); Thread.sleep(5000); } } public static void main(String[] srrt) { for(int i = 0;i < 3;i++) { new Thread(new Runnable() { @Override public void run() { try { test2(1234L); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } }
1、ReentrantLock支持对异常的处理。
2、synchronized不支持对异常的处理,但支持对特定的某一对象值加锁。