您好,登錄后才能下訂單哦!
synchronized 關(guān)鍵字解析
同步鎖依賴于對(duì)象,每個(gè)對(duì)象都有一個(gè)同步鎖。
現(xiàn)有一成員變量 Test,當(dāng)線程 A 調(diào)用 Test 的 synchronized 方法,線程 A 獲得 Test 的同步鎖,同時(shí),線程 B 也去調(diào)用 Test 的 synchronized 方法,此時(shí)線程 B 無(wú)法獲得 Test 的同步鎖,必須等待線程 A 釋放 Test 的同步鎖才能獲得從而執(zhí)行對(duì)應(yīng)方法的代碼。
綜上,正確使用 synchronized 關(guān)鍵字可確保原子性。
synchronized 關(guān)鍵字的特性應(yīng)用
特性 1:
當(dāng)線程 A 調(diào)用某對(duì)象的synchronized 方法 或者 synchronized 代碼塊時(shí),若同步鎖未釋放,其他線程調(diào)用同一對(duì)象的synchronized 方法 或者 synchronized 代碼塊時(shí)將被阻塞,直至線程 A 釋放該對(duì)象的同步鎖。
DEMO1,synchronized 方法:
public class Test { private static class Counter { public synchronized void count() { for (int i = 0; i < 6; i++) { System.out.println(Thread.currentThread().getName() + ", i = " + i); } } } private static class MyThread extends Thread { private Counter mCounter; public MyThread(Counter counter) { mCounter = counter; } @Override public void run() { super.run(); mCounter.count(); } } public static void main(String[] var0) { Counter counter = new Counter(); // 注:myThread1 和 myThread2 是調(diào)用同一個(gè)對(duì)象 counter MyThread myThread1 = new MyThread(counter); MyThread myThread2 = new MyThread(counter); myThread1.start(); myThread2.start(); } }
DEMO1 輸出:
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1, i = 0 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-1, i = 4 Thread-1, i = 5
DEMO2,synchronized 代碼塊:
public class Test { private static class Counter { public void count() { synchronized (this) { for (int i = 0; i < 6; i++) { System.out.println(Thread.currentThread().getName() + ", i = " + i); } } } } private static class MyThread extends Thread { private Counter mCounter; public MyThread(Counter counter) { mCounter = counter; } @Override public void run() { super.run(); mCounter.count(); } } public static void main(String[] var0) { Counter counter = new Counter(); MyThread myThread1 = new MyThread(counter); MyThread myThread2 = new MyThread(counter); myThread1.start(); myThread2.start(); } }
DEMO2 輸出:
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1, i = 0 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-1, i = 4 Thread-1, i = 5
可見,當(dāng)同步鎖未釋放時(shí),其他線程將被阻塞,直至獲得同步鎖。
而且 DEMO1 和 DEMO2 的輸出結(jié)果是一樣的,synchronized 方法 和 synchronized 代碼塊的不同之處在于 synchronized 方法 作用域較大,作用于整個(gè)方法,而 synchronized 代碼塊 可控制具體的作用域,更精準(zhǔn)控制提高效率。(畢竟阻塞的都是時(shí)間?。?/p>
DEMO3,僅修改 main 方法:
public static void main(String[] var0) { // 注意:myThread1 和 myThread2 傳入的 Counter 是兩個(gè)不同的對(duì)象 MyThread myThread1 = new MyThread(new Counter()); MyThread myThread2 = new MyThread(new Counter()); myThread1.start(); myThread2.start(); }
DEMO3 輸出:
Thread-0, i = 0 Thread-1, i = 0 Thread-0, i = 1 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-0, i = 2 Thread-1, i = 4 Thread-0, i = 3 Thread-1, i = 5 Thread-0, i = 4 Thread-0, i = 5
同步鎖基于對(duì)象,只要鎖的來(lái)源一致,即可達(dá)到同步的作用。所以,但對(duì)象不一樣,則不能達(dá)到同步效果。
特性 2:
當(dāng)線程 A 調(diào)用某對(duì)象的synchronized 方法 或者 synchronized 代碼塊時(shí),若同步鎖未釋放,其他線程調(diào)用同一對(duì)象的其他 synchronized 方法 或者 synchronized 代碼塊時(shí)將被阻塞,直至線程 A 釋放該對(duì)象的同步鎖。(注意:重點(diǎn)是其他)
DEMO4,僅修改 doOtherThings 方法的修飾:
public class Test { private static class Counter { public synchronized void count() { System.out.println(Thread.currentThread().getName() + " sleep"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " awake"); } public synchronized void doOtherThings(){ System.out.println(Thread.currentThread().getName() + " doOtherThings"); } } public static void main(String[] var0) { final Counter counter = new Counter(); new Thread(new Runnable() { @Override public void run() { counter.count(); } }).start(); new Thread(new Runnable() { @Override public void run() { counter.doOtherThings(); } }).start(); } }
DEMO4 輸出:
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
可見,synchronized 獲得的同步鎖并非僅僅鎖住代碼,而是鎖住整個(gè)對(duì)象。
此時(shí)應(yīng)提及 happens-before 原則,正因 happens-before 原則的存在才有此現(xiàn)象的發(fā)生。
happens-before 原則的其中一條:
管理鎖定原則:一個(gè) unLock 操作先行發(fā)生于后面對(duì)同一個(gè)鎖的 lock 操作。
(此處暫不作過多解釋,解釋起來(lái)能再寫一篇文章了)
DEMO5,僅修改 doOtherThings 方法:
public void doOtherThings(){ synchronized (this){ System.out.println(Thread.currentThread().getName() + " doOtherThings"); } }
DEMO5 輸出:
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
DEMO4 和 DEMO5 的輸出結(jié)果竟然一致!沒錯(cuò),因?yàn)樗麄兊耐芥i來(lái)源一致(都是本實(shí)例自己),所以可以達(dá)到同步效果。
// 這兩個(gè) synchronized 鎖的是同一個(gè)對(duì)象 public synchronized void count(){}; public void doOtherThings(){ synchronized (this){} }
DEMO6,去掉 doOtherThings 方法的同步關(guān)鍵字:
public void doOtherThings(){ System.out.println(Thread.currentThread().getName() + " doOtherThings"); }
DEMO6 輸出:
Thread-0 sleep Thread-1 doOtherThings Thread-0 awake
當(dāng)線程 A 調(diào)用某對(duì)象的synchronized 方法 或者 synchronized 代碼塊時(shí),無(wú)論同步鎖是否釋放,其他線程調(diào)用同一對(duì)象的其他 非 synchronized 方法 或者 非 synchronized 代碼塊時(shí)可立即調(diào)用。
實(shí)例鎖和全局鎖
以上 DEMO 實(shí)現(xiàn)的都是實(shí)例鎖。鎖住(作用域)的是具體某一對(duì)象實(shí)例。
什么是全局鎖?
鎖住整個(gè) Class,而非某個(gè)對(duì)象或?qū)嵗?/p>
注:?jiǎn)卫偷膶?shí)例鎖不屬于全局鎖。
全局鎖的實(shí)現(xiàn):
靜態(tài) synchronized 方法
DEMO7:
public class Test { private static class Counter { public static synchronized void count() { System.out.println(Thread.currentThread().getName() + " sleep"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " awake"); } public static synchronized void doOtherThings(){ System.out.println(Thread.currentThread().getName() + " doOtherThings"); } } public static void main(String[] var0) { new Thread(new Runnable() { @Override public void run() { Counter.count(); } }).start(); new Thread(new Runnable() { @Override public void run() { Counter.doOtherThings(); } }).start(); } }
DEMO7 輸出:
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
static 聲明的方法為全局方法,與對(duì)象實(shí)例化無(wú)關(guān),所以 static synchronized 方法為全局同步方法,與對(duì)象實(shí)例化無(wú)關(guān)。
synchronized 具體 Class 的代碼塊
DEMO8:
public class Test { private static class Counter { public static synchronized void count() { System.out.println(Thread.currentThread().getName() + " sleep"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " awake"); } public void doOtherThings(){ synchronized (Counter.class){ System.out.println(Thread.currentThread().getName() + " doOtherThings"); } } } public static void main(String[] var0) { new Thread(new Runnable() { @Override public void run() { Counter.count(); } }).start(); new Thread(new Runnable() { @Override public void run() { Counter counter = new Counter(); counter.doOtherThings(); } }).start(); } }
DEMO8 輸出:
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
synchronized (Counter.class) 獲得的同步鎖是全局的,static synchronized 獲得的同步鎖也是全局的,同一個(gè)鎖,所以達(dá)到同步效果。
區(qū)分 synchronized (this) 與 synchronized (Class.class)
DEMO9:
public class Test { private static class Counter { public void count() { synchronized (this){ System.out.println(Thread.currentThread().getName() + " sleep"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " awake"); } } public void doOtherThings(){ synchronized (Counter.class){ System.out.println(Thread.currentThread().getName() + " doOtherThings"); } } } public static void main(String[] var0) { final Counter counter = new Counter(); new Thread(new Runnable() { @Override public void run() { counter.count(); } }).start(); new Thread(new Runnable() { @Override public void run() { counter.doOtherThings(); } }).start(); } }
DEMO9 輸出:
Thread-0 sleep Thread-1 doOtherThings Thread-0 awake
synchronized (this) 獲得的是具體對(duì)象實(shí)例 counter 的鎖,而 synchronized (Counter.class) 獲得的是全局鎖,兩把不同的鎖,所以不能達(dá)到同步效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。