Synchronized 鎖重入分析及Demo實現
阿新 • • 發佈:2018-12-11
關鍵字synchronized擁有鎖重入的功能,也就是在使用synchronized時,當一個執行緒得到一個物件鎖後,再次請求此物件鎖時是再次得到該物件的鎖的,這也證明在一個synchronized方法 / 塊內部呼叫本類的其他synchronized方法 / 塊時,是永遠可以得到鎖的。
“可重入鎖”的概念是:自己可以再次獲取自己的內部鎖。
Demo程式碼如下:
public class DemoServiceReentrenceLock { synchronized public void service1() { try { System.out.println(Thread.currentThread().getName()+ " - " +"service1 獲取鎖" + " - " + System.nanoTime()); System.out.println(Thread.currentThread().getName()+ " - " +"service1" + " - " + System.currentTimeMillis()); Thread.sleep(5000); service2(); System.out.println(Thread.currentThread().getName()+ " - " +"service1 釋放鎖" + " - " + System.nanoTime()); } catch (Exception e) { // TODO: handle exception } } synchronized public void service2() { try { System.out.println(Thread.currentThread().getName()+ " - " +"service2 獲取鎖" + " - " + System.nanoTime()); System.out.println(Thread.currentThread().getName()+ " - " +"service2" + " - " + System.currentTimeMillis()); // Thread.sleep(3000); service3(); System.out.println(Thread.currentThread().getName()+ " - " +"service2 釋放鎖" + " - " + System.nanoTime()); } catch (Exception e) { // TODO: handle exception } } synchronized public void service3() { System.out.println(Thread.currentThread().getName()+ " - " +"service3 獲取鎖" + " - " + System.nanoTime()); System.out.println(Thread.currentThread().getName()+ " - " +"service3" + " - " + System.currentTimeMillis()); System.out.println(Thread.currentThread().getName()+ " - " +"service3釋放鎖" + " - " + System.nanoTime()); } }
public class TestDemoServiceReentrenceLock extends Thread{ private DemoServiceReentrenceLock service; public TestDemoServiceReentrenceLock(DemoServiceReentrenceLock dLock) { this.service = dLock; } @Override public void run() { // TODO Auto-generated method stub super.run(); /* * 若在此處例項化物件,建立新執行緒時會建立新物件,每個執行緒操作的是不同的物件 */ // DemoServiceReentrenceLock service = new DemoServiceReentrenceLock(); service.service1(); } public static void main(String[] args) { DemoServiceReentrenceLock demoServiceReentrenceLock = new DemoServiceReentrenceLock(); TestDemoServiceReentrenceLock demo = new TestDemoServiceReentrenceLock(demoServiceReentrenceLock); // demo.start(); Thread t1 = new Thread(demo); Thread t2 = new Thread(demo); Thread t3 = new Thread(demo); t1.start(); t2.start(); t3.start(); } }
//執行結果: Thread-1 - service1 獲取鎖 - 463506790016015 Thread-1 - service1 - 1540134076620 Thread-1 - service2 獲取鎖 - 463511789963967 Thread-1 - service2 - 1540134081620 Thread-1 - service3 獲取鎖 - 463511790385900 Thread-1 - service3 - 1540134081620 Thread-1 - service3釋放鎖 - 463511790751518 Thread-1 - service2 釋放鎖 - 463511790895718 Thread-1 - service1 釋放鎖 - 463511791731904 Thread-3 - service1 獲取鎖 - 463511792423465 Thread-3 - service1 - 1540134081622 Thread-3 - service2 獲取鎖 - 463516794723832 Thread-3 - service2 - 1540134086625 Thread-3 - service3 獲取鎖 - 463516794978527 Thread-3 - service3 - 1540134086625 Thread-3 - service3釋放鎖 - 463516795099263 Thread-3 - service2 釋放鎖 - 463516795154724 Thread-3 - service1 釋放鎖 - 463516795207199 Thread-2 - service1 獲取鎖 - 463516796485370 Thread-2 - service1 - 1540134086627 Thread-2 - service2 獲取鎖 - 463521795939716 Thread-2 - service2 - 1540134091627 Thread-2 - service3 獲取鎖 - 463521796082209 Thread-2 - service3 - 1540134091627 Thread-2 - service3釋放鎖 - 463521796205930 Thread-2 - service2 釋放鎖 - 463521796272484 Thread-2 - service1 釋放鎖 - 463521796344157