synchronized 關(guān)鍵字解析
同步鎖依賴于對象,每個對象都有一個同步鎖。
現(xiàn)有一成員變量 Test,當線程 A 調(diào)用 Test 的 synchronized 方法,線程 A 獲得 Test 的同步鎖,同時,線程 B 也去調(diào)用 Test 的 synchronized 方法,此時線程 B 無法獲得 Test 的同步鎖,必須等待線程 A 釋放 Test 的同步鎖才能獲得從而執(zhí)行對應(yīng)方法的代碼。
綜上,正確使用 synchronized 關(guān)鍵字可確保原子性。
synchronized 關(guān)鍵字的特性應(yīng)用
特性 1:
當線程 A 調(diào)用某對象的synchronized 方法 或者 synchronized 代碼塊時,若同步鎖未釋放,其他線程調(diào)用同一對象的synchronized 方法 或者 synchronized 代碼塊時將被阻塞,直至線程 A 釋放該對象的同步鎖。
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)用同一個對象 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
可見,當同步鎖未釋放時,其他線程將被阻塞,直至獲得同步鎖。
而且 DEMO1 和 DEMO2 的輸出結(jié)果是一樣的,synchronized 方法 和 synchronized 代碼塊的不同之處在于 synchronized 方法 作用域較大,作用于整個方法,而 synchronized 代碼塊 可控制具體的作用域,更精準控制提高效率。(畢竟阻塞的都是時間?。?/p>
DEMO3,僅修改 main 方法:
public static void main(String[] var0) { // 注意:myThread1 和 myThread2 傳入的 Counter 是兩個不同的對象 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
同步鎖基于對象,只要鎖的來源一致,即可達到同步的作用。所以,但對象不一樣,則不能達到同步效果。
特性 2:
當線程 A 調(diào)用某對象的synchronized 方法 或者 synchronized 代碼塊時,若同步鎖未釋放,其他線程調(diào)用同一對象的其他 synchronized 方法 或者 synchronized 代碼塊時將被阻塞,直至線程 A 釋放該對象的同步鎖。(注意:重點是其他)
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 獲得的同步鎖并非僅僅鎖住代碼,而是鎖住整個對象。