synchronized 和 Lock 有什么区别?
1. 原始构成
synchronized 是关键字属于 JVM 层面。
* monitorenter(底层是通过monitor对象来完成,其实 wait/notify 等方法也依赖monitor对象,只有在同步块或方法中才能调 wait/notify等方法
* monitorexit
Lock.是具体类(java.util.concurrent.locks.Lock)是 api 层面的锁
2. 使用方法
synchronized 不需要用户去主动释放锁,当 synchronized 代码执行完后系统会自动让线程释放对锁的占用
ReentrantLock 则需要用户去手动释放锁若没有主动释放锁,就有可能导致出现死锁现象。
* 需要 lock() 和 unlock()方法配合 try/finally 语句块来完成。
3. 等待是否可中断
synchronized 不可中断,除非抛出异常或者正常运行完成。
ReentrantLock 可中断:3.1 设置超时方法tryLock(long timeout, TimeUnit unit)
3.2 lockInterruptibly()放代码块中,调用interrupt() 方法可中断
4. 加锁是否公平
synchronized非公平锁
ReentrantLock两者都可以,默认非公平锁,构造方法可以传入boolean值,true为公平锁,false为非公平锁
5. 锁绑定多个条件Condition
synchronized没有
ReentrantLock 用来实现分组唤醒需要唤醒的线程们,可以精确唤醒,而不是像synchronized要么随机唤醒一个线程,要么唤醒全部线程。
示例:A、B、C 三个线程顺序工作,分别打印 5、10、15次
1 class ShareResource { 2 private int number = 1; // A:1 B:2 C:3 3 private Lock lock = new ReentrantLock(); 4 private Condition condition1 = lock.newCondition(); 5 private Condition condition2 = lock.newCondition(); 6 private Condition condition3 = lock.newCondition(); 7 8 public void aPrint() { 9 lock.lock(); 10 try { 11 while (number != 1) { 12 condition1.await(); 13 } 14 for (int i = 1; i <= 5; i++) { 15 System.out.println(Thread.currentThread().getName() + " " + i); 16 } 17 number = 2; 18 condition2.signal(); 19 } catch (InterruptedException e) { 20 e.printStackTrace(); 21 } finally { 22 lock.unlock(); 23 } 24 } 25 26 public void bPrint() { 27 lock.lock(); 28 try { 29 while (number != 2) { 30 condition2.await(); 31 } 32 for (int i = 1; i <= 10; i++) { 33 System.out.println(Thread.currentThread().getName() + " " + i); 34 } 35 number = 3; 36 condition3.signal(); 37 } catch (InterruptedException e) { 38 e.printStackTrace(); 39 } finally { 40 lock.unlock(); 41 } 42 } 43 44 public void cPrint() { 45 lock.lock(); 46 try { 47 while (number != 3) { 48 condition3.await(); 49 } 50 for (int i = 1; i <= 15; i++) { 51 System.out.println(Thread.currentThread().getName() + " " + i); 52 } 53 number = 1; 54 condition1.signal(); 55 } catch (InterruptedException e) { 56 e.printStackTrace(); 57 } finally { 58 lock.unlock(); 59 } 60 } 61 } 62 63 public class SyncAndReentrantLockDemo { 64 65 public static void main(String[] args) { 66 ShareResource shareResource = new ShareResource(); 67 68 new Thread(() -> { 69 for (int i = 0; i < 10; i++) { 70 shareResource.aPrint(); 71 } 72 }, "A").start(); 73 74 new Thread(() -> { 75 for (int i = 0; i < 10; i++) { 76 shareResource.bPrint(); 77 } 78 }, "B").start(); 79 80 new Thread(() -> { 81 for (int i = 0; i < 10; i++) { 82 shareResource.cPrint(); 83 } 84 }, "C").start(); 85 } 86 }