1.Synchronize和ReentrantLock區別php
4.ReentrantLock鎖機制測試案例分析java
5.問答測試題git
01.Synchronize深刻解析github
02.Synchronize和ReentrantLock區別面試
03.死鎖的發生,定位與修復segmentfault
synchronized既能夠修飾方法,也能夠修飾代碼塊。安全
//synchronized修飾一個方法時,這個方法叫同步方法。 public synchronized void test() { //方法體`` } synchronized(Object) { //括號中表示須要鎖的對象. //線程執行的時候會對Object上鎖 }
ReentrantLock使用markdown
private ReentrantLock lock = new ReentrantLock(); public void run() { lock.lock(); try{ for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+":"+i); } }finally{ lock.unlock(); } }
具體來講,假如業務代碼中有兩個線程,Thread1 Thread2。假設 Thread1 獲取了對象object的鎖,Thread2將等待Thread1釋放object的鎖。多線程
synchronized的鎖是非公平鎖,ReentrantLock默認狀況下也是非公平鎖,但能夠經過帶布爾值的構造函數要求使用公平鎖。jvm
ReentrantLock是java.util.concurrent包下提供的一套互斥鎖,相比Synchronized,ReentrantLock類提供了一些高級功能,主要有如下3項:
使用方法代碼以下
private ReentrantLock lock = new ReentrantLock(); public void run() { lock.lock(); try{ for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+":"+i); } }finally{ lock.unlock(); } }
代碼以下所示
private void test2() { Runnable t1 = new MyThread(); new Thread(t1,"t1").start(); new Thread(t1,"t2").start(); } class MyThread implements Runnable { private ReentrantLock lock = new ReentrantLock(); public void run() { lock.lock(); try{ for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+":"+i); } }finally{ lock.unlock(); } } } //打印值以下所示 10-17 17:06:59.222 6531-6846/com.yc.cn.ycbaseadapter I/System.out: t1:0 10-17 17:06:59.222 6531-6846/com.yc.cn.ycbaseadapter I/System.out: t1:1 10-17 17:06:59.222 6531-6846/com.yc.cn.ycbaseadapter I/System.out: t1:2 10-17 17:06:59.222 6531-6846/com.yc.cn.ycbaseadapter I/System.out: t1:3 10-17 17:06:59.222 6531-6846/com.yc.cn.ycbaseadapter I/System.out: t1:4 10-17 17:06:59.224 6531-6847/com.yc.cn.ycbaseadapter I/System.out: t2:0 10-17 17:06:59.225 6531-6847/com.yc.cn.ycbaseadapter I/System.out: t2:1 10-17 17:06:59.225 6531-6847/com.yc.cn.ycbaseadapter I/System.out: t2:2 10-17 17:06:59.225 6531-6847/com.yc.cn.ycbaseadapter I/System.out: t2:3 10-17 17:06:59.225 6531-6847/com.yc.cn.ycbaseadapter I/System.out: t2:4
使用場景代碼展現【摘自ThreadPoolExecutor類,這個類中不少地方用到了這個鎖。本身能夠查看】:
/** * Rolls back the worker thread creation. * - removes worker from workers, if present * - decrements worker count * - rechecks for termination, in case the existence of this
*/ private void addWorkerFailed(Worker w) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { if (w != null) workers.remove(w); decrementWorkerCount(); tryTerminate(); } finally { mainLock.unlock(); } } ```
公平性是指在競爭場景中,當公平性爲真時,會傾向於將鎖賦予等待時間最久的線程。公平性是減小線程「飢餓」(個別線程長期等待鎖,但始終沒法獲取)狀況發生的一個辦法。
private void test3() { Service service = new Service(); ThreadClass tcArray[] = new ThreadClass[10]; for(int i=0;i<10;i++){ tcArray[i] = new ThreadClass(service); tcArray[i].start(); } } public class Service { ReentrantLock lock = new ReentrantLock(true); Service() { } void getThreadName() { System.out.println(Thread.currentThread().getName() + " 已經被鎖定"); } } public class ThreadClass extends Thread{ private Service service; ThreadClass(Service service) { this.service = service; } public void run(){ System.out.println(Thread.currentThread().getName() + " 搶到了鎖"); service.lock.lock(); service.getThreadName(); service.lock.unlock(); } } //當ReentrantLock設置true,也就是公平鎖時 10-17 19:32:22.422 6459-6523/com.yc.cn.ycbaseadapter I/System.out: Thread-5 搶到了鎖 10-17 19:32:22.422 6459-6523/com.yc.cn.ycbaseadapter I/System.out: Thread-5 已經被鎖定 10-17 19:32:22.424 6459-6524/com.yc.cn.ycbaseadapter I/System.out: Thread-6 搶到了鎖 10-17 19:32:22.424 6459-6524/com.yc.cn.ycbaseadapter I/System.out: Thread-6 已經被鎖定 10-17 19:32:22.427 6459-6525/com.yc.cn.ycbaseadapter I/System.out: Thread-7 搶到了鎖 10-17 19:32:22.427 6459-6526/com.yc.cn.ycbaseadapter I/System.out: Thread-8 搶到了鎖 10-17 19:32:22.427 6459-6525/com.yc.cn.ycbaseadapter I/System.out: Thread-7 已經被鎖定 10-17 19:32:22.427 6459-6526/com.yc.cn.ycbaseadapter I/System.out: Thread-8 已經被鎖定 10-17 19:32:22.427 6459-6527/com.yc.cn.ycbaseadapter I/System.out: Thread-9 搶到了鎖 10-17 19:32:22.427 6459-6527/com.yc.cn.ycbaseadapter I/System.out: Thread-9 已經被鎖定 10-17 19:32:22.428 6459-6528/com.yc.cn.ycbaseadapter I/System.out: Thread-10 搶到了鎖 10-17 19:32:22.428 6459-6528/com.yc.cn.ycbaseadapter I/System.out: Thread-10 已經被鎖定 10-17 19:32:22.429 6459-6529/com.yc.cn.ycbaseadapter I/System.out: Thread-11 搶到了鎖 10-17 19:32:22.429 6459-6529/com.yc.cn.ycbaseadapter I/System.out: Thread-11 已經被鎖定 10-17 19:32:22.430 6459-6530/com.yc.cn.ycbaseadapter I/System.out: Thread-12 搶到了鎖 10-17 19:32:22.430 6459-6530/com.yc.cn.ycbaseadapter I/System.out: Thread-12 已經被鎖定 10-17 19:32:22.431 6459-6532/com.yc.cn.ycbaseadapter I/System.out: Thread-14 搶到了鎖 10-17 19:32:22.431 6459-6532/com.yc.cn.ycbaseadapter I/System.out: Thread-14 已經被鎖定 10-17 19:32:22.432 6459-6531/com.yc.cn.ycbaseadapter I/System.out: Thread-13 搶到了鎖 10-17 19:32:22.433 6459-6531/com.yc.cn.ycbaseadapter I/System.out: Thread-13 已經被鎖定 //當ReentrantLock設置false,也就是非公平鎖時 10-17 19:34:58.102 7089-7183/com.yc.cn.ycbaseadapter I/System.out: Thread-5 搶到了鎖 10-17 19:34:58.102 7089-7184/com.yc.cn.ycbaseadapter I/System.out: Thread-6 搶到了鎖 10-17 19:34:58.103 7089-7183/com.yc.cn.ycbaseadapter I/System.out: Thread-5 已經被鎖定 10-17 19:34:58.103 7089-7185/com.yc.cn.ycbaseadapter I/System.out: Thread-7 搶到了鎖 10-17 19:34:58.103 7089-7185/com.yc.cn.ycbaseadapter I/System.out: Thread-7 已經被鎖定 10-17 19:34:58.103 7089-7184/com.yc.cn.ycbaseadapter I/System.out: Thread-6 已經被鎖定 10-17 19:34:58.104 7089-7186/com.yc.cn.ycbaseadapter I/System.out: Thread-8 搶到了鎖 10-17 19:34:58.105 7089-7186/com.yc.cn.ycbaseadapter I/System.out: Thread-8 已經被鎖定 10-17 19:34:58.108 7089-7187/com.yc.cn.ycbaseadapter I/System.out: Thread-9 搶到了鎖 10-17 19:34:58.108 7089-7187/com.yc.cn.ycbaseadapter I/System.out: Thread-9 已經被鎖定 10-17 19:34:58.111 7089-7188/com.yc.cn.ycbaseadapter I/System.out: Thread-10 搶到了鎖 10-17 19:34:58.112 7089-7188/com.yc.cn.ycbaseadapter I/System.out: Thread-10 已經被鎖定 10-17 19:34:58.112 7089-7189/com.yc.cn.ycbaseadapter I/System.out: Thread-11 搶到了鎖 10-17 19:34:58.113 7089-7189/com.yc.cn.ycbaseadapter I/System.out: Thread-11 已經被鎖定 10-17 19:34:58.113 7089-7193/com.yc.cn.ycbaseadapter I/System.out: Thread-14 搶到了鎖 10-17 19:34:58.113 7089-7193/com.yc.cn.ycbaseadapter I/System.out: Thread-14 已經被鎖定 10-17 19:34:58.115 7089-7190/com.yc.cn.ycbaseadapter I/System.out: Thread-12 搶到了鎖 10-17 19:34:58.115 7089-7190/com.yc.cn.ycbaseadapter I/System.out: Thread-12 已經被鎖定 10-17 19:34:58.116 7089-7191/com.yc.cn.ycbaseadapter I/System.out: Thread-13 搶到了鎖 10-17 19:34:58.116 7089-7191/com.yc.cn.ycbaseadapter I/System.out: Thread-13 已經被鎖定
1 用法比較
2 特性比較
ReentrantLock的優點體如今:
3 注意事項
在使用ReentrantLock類的時,必定要注意三點: