synchronized关键字的使用

(已迁移)

--  类锁与对象锁的区别

    类锁所有对象一把锁

    对象锁一个对象一把锁,多个对象对把锁

--  同步是对同一把锁而言的,同步这个概念是在多个线程争夺同一把锁的时候才能实现的,如果多个线程争夺不同的锁,那多个线程是不能同步的

    两个线程一个取对象锁,一个取类锁,则不能同步

    两个线程一个取a对象锁,一个取b对象锁,则不能同步

 

--  synchronized实现同步的原因在于,保证了拿到锁的线程一定可以一次性把他调用的方法或代码块执行完

 

--  synchronized用于方法:

 1 public class B {
 2     //修饰静态方法,或者修饰普通方法
 3     synchronized public static void mB(String value) throws InterruptedException{
 4         for(int i=0;i<1000;++i){
 5             System.out.println(value);
 6         }
 7     }
 8 
 9     synchronized public void mC(String value) throws InterruptedException{
10         for(int i=0;i<1000;++i){
11             System.out.println(value);
12         }
13     }
14 }

--  synchronized用于代码块:

 1 public class A {
 2     public static void test(){
 3         //对类进行同步
 4         synchronized(A.class){
 5             System.out.println("smlz");
 6         }
 7     }
 8 
 9     public void test2(){
10         //对当前对象进行同步
11         synchronized(this){
12             System.out.println("smlz");
13         }
14     }
15 }

--  类锁

  类锁,用来锁类的,使持有者可以同步调用静态方法。当synchronized指定修饰静态方法或者class对象的时候,拿到的就是类锁。类锁是所有对象共抢一把锁。

 1 public class B {
 2     synchronized public static void mB(String value) throws InterruptedException{
 3         for(int i=0;i<1000;++i){
 4             System.out.print(value);
 5         }
 6     }
 7 
 8     public void mC(String value){
 9         synchronized(B.class){
10             for(int i=0;i<1000;++i){
11                 System.out.print(value);
12             }
13         }
14     }
15 }

--  对象锁

  用来锁对象,每个对象各有一把。

 1 public class C {
 2     synchronized public void mB(String value) throws InterruptedException{
 3         for(int i=0;i<1000;++i){
 4             System.out.print(value);
 5         }
 6     }
 7 
 8     public void mC(String value){
 9         synchronized(this){
10             for(int i=0;i<1000;++i){
11                 System.out.print(value);
12             }
13         }
14     }
15 }

--  测试案例

  1、两个线程调用同一个对象b的MB方法,最终1000次1和1000次2的结果是分开的,即实现了同步

 1 public class B {
 2     synchronized public void mB(String value) throws InterruptedException{
 3         for(int i=0;i<1000;++i){
 4             System.out.print(value);
 5         }
 6     }
 7 
 8     public void mC(String value) throws InterruptedException{
 9         synchronized(this){
10             for(int i=0;i<1000;++i){
11                 System.out.print(value);
12             }
13         }
14     }
15 }
16 
17 
18 public class ApplicationDemo {
19     public static void main(String[] args){
20         B b = new B();
21 
22         Thread thread1 = new Thread(new Runnable(){
23             @Override
24             public void run(){
25                 try{
26                     b.mB("1");
27                 }catch(InterruptedException e){
28                     e.printStackTrace();
29                 }
30             }
31         });
32 
33         Thread thread2 = new Thread(new Runnable(){
34             @Override
35             public void run(){
36                 try{
37                     b.mC("2");
38                 }catch(InterruptedException e){
39                     e.printStackTrace();
40                 }
41             }
42         });
43 
44         thread1.start();
45         thread2.start();
46     }
47 }

 

  2、使用类锁进行测试,同样1和2是分开的

 1 public class B {
 2     synchronized public static void mB(String value) throws InterruptedException{
 3         for(int i=0;i<1000;++i){
 4             System.out.print(value);
 5         }
 6     }
 7 
 8     public static void mC(String value) throws InterruptedException{
 9         synchronized(B.class){
10             for(int i=0;i<1000;++i){
11                 System.out.print(value);
12             }
13         }
14     }
15 }
16 
17 
18 public class ApplicationDemo {
19     public static void main(String[] args){
20 
21         Thread thread1 = new Thread(new Runnable(){
22             @Override
23             public void run(){
24                 try{
25                     B.mB("1");
26                 }catch(InterruptedException e){
27                     e.printStackTrace();
28                 }
29             }
30         });
31 
32         Thread thread2 = new Thread(new Runnable(){
33             @Override
34             public void run(){
35                 try{
36                     B.mC("2");
37                 }catch(InterruptedException e){
38                     e.printStackTrace();
39                 }
40             }
41         });
42 
43         thread1.start();
44         thread2.start();
45     }
46 }

 

  3、但如果类锁和对象锁同时存在,则无法保证同步,即1和2将交替打出

 1 public class B {
 2     synchronized public static void mB(String value) throws InterruptedException{
 3         for(int i=0;i<1000;++i){
 4             System.out.print(value);
 5         }
 6     }
 7 
 8     public void mC(String value) throws InterruptedException{
 9         synchronized(this){
10             for(int i=0;i<1000;++i){
11                 System.out.print(value);
12             }
13         }
14     }
15 }
16 
17 
18 public class ApplicationDemo {
19     public static void main(String[] args){
20 
21         Thread thread1 = new Thread(new Runnable(){
22             @Override
23             public void run(){
24                 try{
25                     B.mB("1");
26                 }catch(InterruptedException e){
27                     e.printStackTrace();
28                 }
29             }
30         });
31 
32         B b = new B();
33         Thread thread2 = new Thread(new Runnable(){
34             @Override
35             public void run(){
36                 try{
37                     b.mC("2");
38                 }catch(InterruptedException e){
39                     e.printStackTrace();
40                 }
41             }
42         });
43 
44         thread1.start();
45         thread2.start();
46     }
47 }

 

 

(仅个人整理,非原创)

(感谢大佬原创,原文来自:https://blog.csdn.net/TesuZer/article/details/80874195)

 

posted @ 2020-10-12 22:48  α_伊卡洛斯  阅读(134)  评论(0)    收藏  举报