須要明確的幾個問題:ide
一、使用在方法上synchronized aMethod(){...}函數
使用相同的 object測試
public class synchTest { private String a= ""; private List<String> b= new ArrayList<>(); // 方法一 public void job() { System.out.println("job ....."); synchronized (b){ System.out.println("job 使用鎖中 ...."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job end....."); } // 方法二 public synchronized void job2(){ System.out.println("job2 ....."); synchronized (b){ System.out.println("job22 使用鎖中 ..."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job2 end....."); } public static void main(String[] args) { final synchTest rs = new synchTest(); new Thread() { public void run() { rs.job(); } }.start(); new Thread() { public void run() { rs.job2(); } }.start(); } } 結果: job ..... job 使用鎖中 .... job2 ..... job end..... job22 使用鎖中 ... job2 end.....
使用不一樣的objectthis
public class synchTest { private String a= ""; private List<String> b= new ArrayList<>(); // 方法一 public void job() { System.out.println("job ....."); synchronized (a){ System.out.println("job 使用鎖中 ...."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job end....."); } // 方法二 public synchronized void job2(){ System.out.println("job2 ....."); synchronized (b){ System.out.println("job22 使用鎖中 ..."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job2 end....."); } public static void main(String[] args) { final synchTest rs = new synchTest(); new Thread() { public void run() { rs.job(); } }.start(); new Thread() { public void run() { rs.job2(); } }.start(); } } 結果: job ..... job 使用鎖中 .... job2 ..... job22 使用鎖中 ... job end..... job2 end.....
使用this關鍵詞 spa
public class synchTest { private String a= ""; private List<String> b= new ArrayList<>(); // 方法一 public void job() { System.out.println("job ....."); synchronized (this){ System.out.println("job 使用鎖中 ...."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job end....."); } // 方法二 public synchronized void job2(){ System.out.println("job2 ....."); synchronized (b){ System.out.println("job22 使用鎖中 ..."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job2 end....."); } public static void main(String[] args) { final synchTest rs = new synchTest(); new Thread() { public void run() { rs.job(); } }.start(); new Thread() { public void run() { rs.job2(); } }.start(); } } 結果: job ..... job 使用鎖中 .... job end..... job2 ..... job22 使用鎖中 ... job2 end.....
結論:線程
二、使用在方法內部 synchronized(Oject){...}3d
public class synchTest { // 方法一 public synchronized void job() { System.out.println("job ....."); System.out.println("job 使用鎖中 ...."); try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println("job end....."); } // 方法二 public synchronized void job2(){ System.out.println("job2 ....."); System.out.println("job22 使用鎖中 ..."); try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println("job2 end....."); } public static void main(String[] args) { final synchTest rs = new synchTest(); new Thread() { public void run() { rs.job(); } }.start(); new Thread() { public void run() { rs.job2(); } }.start(); } } 結果: job ..... job 使用鎖中 .... job end..... job2 ..... job22 使用鎖中 ... job2 end.....
結論:code
二、使用在方法內部 synchronized(Oject){...}、synchronized aMethod(){...}混用對象
public class synchTest { public String a = ""; // 方法一 public void job() { System.out.println("job ....."); synchronized (a){ System.out.println("job 使用鎖中 ...."); try { Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println("job end....."); } // 方法二 public synchronized void job2(){ System.out.println("job2 ....."); System.out.println("job22 使用鎖中 ..."); try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println("job2 end....."); } public static void main(String[] args) { final synchTest rs = new synchTest(); new Thread() { public void run() { rs.job(); } }.start(); new Thread() { public void run() { rs.job2(); } }.start(); } } 結果: job ..... job 使用鎖中 .... job2 ..... job22 使用鎖中 ... job end..... job2 end.....
結論:blog
對象實例內 synchronized aMethod(){} 與synchronized(Object) 不會相互同步