synchronized 用法详解

1. 加在普通方法上

单例下互斥,非单例无用

 1 public class Test {
 2 
 3     private synchronized void syncMethod1() {
 4         System.out.println("in1:" + System.currentTimeMillis() / 1000);
 5         try {
 6             Thread.sleep(5000);
 7         } catch (InterruptedException e) {
 8             e.printStackTrace();
 9         }
10     }
11 
12     private synchronized void syncMethod2() {
13         System.out.println("in2:" + System.currentTimeMillis() / 1000);
14         try {
15             Thread.sleep(5000);
16         } catch (InterruptedException e) {
17             e.printStackTrace();
18         }
19     }
20 
21     public static void main(String[] args) {
22         final Test test = new Test();
23         new Thread(new Runnable() {
24             public void run() {
25                 test.syncMethod1();
26             }
27         }).start();
28 
29         new Thread(new Runnable() {
30             public void run() {
31                 test.syncMethod2();
32             }
33         }).start();
34 
35     }
36 }
View Code
 result

in1:1562051336
in2:1562051341

 

2.代码块,使用this

  锁住当前对象,同上普通方法锁相同,非单例无用

 1 public class Test {
 2 
 3 
 4     private  void syncMethod1() {
 5         synchronized (this) {
 6             System.out.println("in1:" + System.currentTimeMillis() / 1000);
 7             try {
 8                 Thread.sleep(5000);
 9             } catch (InterruptedException e) {
10                 e.printStackTrace();
11             }
12         }
13 
14     }
15 
16     private void syncMethod2() {
17         synchronized (this) {
18             System.out.println("in2:" + System.currentTimeMillis() / 1000);
19             try {
20                 Thread.sleep(5000);
21             } catch (InterruptedException e) {
22                 e.printStackTrace();
23             }
24         }
25     }
26 
27     public static void main(String[] args) {
28         final Test test = new Test();
29         new Thread(new Runnable() {
30             public void run() {
31                 test.syncMethod1();
32             }
33         }).start();
34 
35         new Thread(new Runnable() {
36             public void run() {
37                 test.syncMethod2();
38             }
39         }).start();
40     }
41 }
View Code
result 
in1:1562051825
in2:1562051830

 

 

3.加在静态方法

同类下互斥,非同类下无用

 

 1 public class Test {
 2 
 3 
 4     private static synchronized void syncMethod1() {
 5         System.out.println("in1:" + System.currentTimeMillis() / 1000);
 6         try {
 7             Thread.sleep(5000);
 8         } catch (InterruptedException e) {
 9             e.printStackTrace();
10         }
11     }
12 
13     private static synchronized void syncMethod2() {
14         System.out.println("in2:" + System.currentTimeMillis() / 1000);
15         try {
16             Thread.sleep(5000);
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         }
20     }
21 
22     public static void main(String[] args) {
23         new Thread(new Runnable() {
24             public void run() {
25                 Test.syncMethod1();
26             }
27         }).start();
28 
29         new Thread(new Runnable() {
30             public void run() {
31                 Test.syncMethod2();
32             }
33         }).start();
34 
35     }
36 }
View Code
resut 
in1:1562051498
in2:1562051503

 

4.代码块,使用类 

使用相同类名互斥,类级别锁,同静态类方法锁,多实例下有效

 1 public class Test {
 2 
 3 
 4     private void syncMethod1() {
 5         synchronized (Test.class) {
 6             System.out.println("in1:" + System.currentTimeMillis() / 1000);
 7             try {
 8                 Thread.sleep(5000);
 9             } catch (InterruptedException e) {
10                 e.printStackTrace();
11             }
12         }
13 
14     }
15 
16     private void syncMethod2() {
17         synchronized (Test.class) {
18             System.out.println("in2:" + System.currentTimeMillis() / 1000);
19             try {
20                 Thread.sleep(5000);
21             } catch (InterruptedException e) {
22                 e.printStackTrace();
23             }
24         }
25     }
26 
27     public static void main(String[] args) {
28 
29         new Thread(new Runnable() {
30             public void run() {
31                 new Test().syncMethod1();
32             }
33         }).start();
34 
35         new Thread(new Runnable() {
36             public void run() {
37                 new Test().syncMethod2();
38             }
39         }).start();
40 
41     }
View Code
result
in1:1562051647
in2:1562051652

 

5.自定义对象锁(非静态)

实例变量对象锁,单例有效,多例无用

 

 1 public class Test {
 2 
 3     private final String[] MY_LOCK = new String[]{};
 4 
 5     private  void syncMethod1() {
 6         synchronized (MY_LOCK) {
 7             System.out.println("in1:" + System.currentTimeMillis() / 1000);
 8             try {
 9                 Thread.sleep(5000);
10             } catch (InterruptedException e) {
11                 e.printStackTrace();
12             }
13         }
14 
15     }
16 
17     private void syncMethod2() {
18         synchronized (MY_LOCK) {
19             System.out.println("in2:" + System.currentTimeMillis() / 1000);
20             try {
21                 Thread.sleep(5000);
22             } catch (InterruptedException e) {
23                 e.printStackTrace();
24             }
25         }
26     }
27 
28     public static void main(String[] args) {
29         final Test test = new Test();
30         new Thread(new Runnable() {
31             public void run() {
32                 test.syncMethod1();
33             }
34         }).start();
35 
36         new Thread(new Runnable() {
37             public void run() {
38                 test.syncMethod2();
39             }
40         }).start();
41     }
42 }
View Code
result
in1:1562052198
in2:1562052203

5.自定义对象锁(静态)

静态对象锁,多例有效

 1 public class Test {
 2 
 3     private static final String[] MY_LOCK = new String[]{};
 4 
 5     private void syncMethod1() {
 6         synchronized (MY_LOCK) {
 7             System.out.println("in1:" + System.currentTimeMillis() / 1000);
 8             try {
 9                 Thread.sleep(5000);
10             } catch (InterruptedException e) {
11                 e.printStackTrace();
12             }
13         }
14 
15     }
16 
17     private void syncMethod2() {
18         synchronized (MY_LOCK) {
19             System.out.println("in2:" + System.currentTimeMillis() / 1000);
20             try {
21                 Thread.sleep(5000);
22             } catch (InterruptedException e) {
23                 e.printStackTrace();
24             }
25         }
26     }
27 
28     public static void main(String[] args) {
29         new Thread(new Runnable() {
30             public void run() {
31                 new Test().syncMethod1();
32             }
33         }).start();
34 
35         new Thread(new Runnable() {
36             public void run() {
37                 new Test().syncMethod2();
38             }
39         }).start();
40     }
41 }
View Code
result 

in1:1562052289
in2:1562052294

 

posted @ 2019-07-02 15:27  悠拽的无双文士  阅读(218)  评论(0编辑  收藏  举报