须要明确的几个问题: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) 不会相互同步