synchronized

synchronized 修饰静态方法、普通方法与代码块的区别

类锁:所有对象共用一个锁

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

一、synchronized修饰普通方法(对象锁)

class B{
    
    synchronized public void mB(String value) {
        for (int i = 0; i < 10; i++) {
            System.out.println(value);
            try{
                Thread.sleep(150);
            }catch(Exception e){
                
            }
        }
    }

    synchronized public void mC(String value) {
        for (int i = 0; i < 10; i++) {
            System.out.println(value);
            try{
                Thread.sleep(150);
            }catch(Exception e){
                
            }
        }
    }
    
}

public class Main {
    
    public static void main(String[] agrs){
        System.out.println("Main 线程 开始运行!");
        final B b1 = new B();
        Thread t1 = new Thread(){
            @Override
            public void run(){
                System.out.println("t1 开始运行!");
                b1.mB("HAHA");
                System.out.println("t1 结束运行!");
            }
        };
        t1.start();
        Thread t2 = new Thread(){
            @Override
            public void run(){
                System.out.println("t2 开始运行!");
                b1.mC("XIXI");
                System.out.println("t2 结束运行!");
            }
        };
        t2.start();
           
        System.out.println("Main 线程 结束运行!");
    }
}

结果

Main 线程 开始运行!
Main 线程 结束运行!
t1 开始运行!
HAHA
t2 开始运行!
HAHA
HAHA
HAHA
HAHA
HAHA
HAHA
HAHA
HAHA
HAHA
t1 结束运行!
XIXI
XIXI
XIXI
XIXI
XIXI
XIXI
XIXI
XIXI
XIXI
XIXI
t2 结束运行!

结论:修饰普通方法的时候,锁的是当前对象。
如果创建个新对象b2,则不能保持同步!!
例如下面的代码,无法保持同步!!!

public class Main {
    
    public static void main(String[] agrs){
        System.out.println("Main 线程 开始运行!");
        final B b1 = new B();
        Thread t1 = new Thread(){
            @Override
            public void run(){
                System.out.println("t1 开始运行!");
                b1.mB("HAHA");
                System.out.println("t1 结束运行!");
            }
        };
        t1.start();
        
        final B b2 = new B();
        Thread t2 = new Thread(){
            @Override
            public void run(){
                System.out.println("t2 开始运行!");
                b2.mC("XIXI");
                System.out.println("t2 结束运行!");
            }
        };
        t2.start();
        System.out.println("Main 线程 结束运行!");
    }
}

二、synchronized修饰静态方法(类锁)

B 类修改为如下:

class B{
    
    synchronized static public void mB(String value) {
        for (int i = 0; i < 10; i++) {
            System.out.println(value);
            try{
                Thread.sleep(150);
            }catch(Exception e){
                
            }
        }
    }

    synchronized static public void mC(String value) {
        for (int i = 0; i < 10; i++) {
            System.out.println(value);
            try{
                Thread.sleep(150);
            }catch(Exception e){
                
            }
        }
    }
    
}

结论:就算两个不同的对象,也可以保持同步!!!

三、synchronized修饰代码块(也分类锁、对象锁)

class B{
 public void mB(String value) {
        synchronized(B.class){
            for (int i = 0; i < 10; i++) {
                System.out.println(value);
                try{
                    Thread.sleep(150);
                }catch(Exception e){
                    
                }
            }
        }
        
    }
 public void mC(String value) {
        synchronized(B.class){
            for (int i = 0; i < 10; i++) {
                System.out.println(value);
                try{
                    Thread.sleep(150);
                }catch(Exception e){
                    
                }
            }
        }
    }
}

上面是类锁:所有对象可以保持同步。

class B{
 public void mB(String value) {
        synchronized(this){
            for (int i = 0; i < 10; i++) {
                System.out.println(value);
                try{
                    Thread.sleep(150);
                }catch(Exception e){
                    
                }
            }
        }
        
    }
 public void mC(String value) {
        synchronized(this){
            for (int i = 0; i < 10; i++) {
                System.out.println(value);
                try{
                    Thread.sleep(150);
                }catch(Exception e){
                    
                }
            }
        }
    }
}

上面是对象锁:同一对象可以保持同步,不同对象不可保持同步。
参考链接:https://www.jianshu.com/p/3cfdf32bd37e

自律人的才是可怕的人
原文地址:https://www.cnblogs.com/lovelifest/p/14467487.html