synchronized 使用详解+案例

synchronized 锁的类型

synchronized的锁类型,我觉得主要分为两种,一种是类锁,一种是实例对象锁。

类锁

类锁,顾名思义,就是在这个类上面加锁,也就是说,对于所有加了类锁的方法的时候,一次只有一个线程能够调用一个增加类锁的方法。

加类锁的方式

1、在静态方法上面使用 synchronized 进行修饰
2、加类锁的方式是在代码段中增加 synchronized 片段,如,上述增加类锁的方式就是在某一代码段中增加。 synchronized(Data.class) {}
案例如下:

public class Data {
	public static void main(String[] args) {
        new Thread(() -> {
            Data.method1();
        }, "A").start();
        new Thread(() -> {
            Data.method1();
        }, "B").start();
    }
    public synchronized static void method1() {
        for (int i=0; i<3; i++) {
            System.out.println("method01,  Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized static void method2() {
        for (int i=0; i<3; i++) {
            System.out.println("method2, Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

输入如下:
method01, Thread name: A
method01, Thread name: A
method01, Thread name: A
method02, Thread name: B
method02, Thread name: B
method02, Thread name: B

从上述输出可以看出两个线程访问两个加了类锁的两个方法,一次只能有一个加了类锁的方法被访问。

对象锁

对象是是针对于对象的实例,对于同一个对象的多个实例而言,增加了对象锁的两个方法,同一个实例对象一次只能调用一个增加对象锁的方法。

加对象锁的方式

1、就是在一个普通的方法上面增加 synchronized 关键字。
2、在方法的代码块中使用 synchronized 关键字。格式如:synchronized(this)

public class Data {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(() -> {
            data.method1();
        }, "A").start();
        new Thread(() -> {
            data.method2();
        }, "B").start();
    }
    public synchronized void method1() {
        for (int i=0; i<3; i++) {
            System.out.println("method01,  Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void method2() {
        for (int i=0; i<3; i++) {
            System.out.println("method02, Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

输出如下:
method01, Thread name: A
method01, Thread name: A
method01, Thread name: A
method02, Thread name: B
method02, Thread name: B
method02, Thread name: B

证实了上述的结论。

对于类锁和对象锁的调用呢?

如果两个线程一个调用了类锁一个调用了对象锁呢,顺序是怎么样的?
案例:

public class Data {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(() -> {
            Data.method1();
        }, "A").start();
        new Thread(() -> {
            data.method2();
        }, "B").start();
    }
    public synchronized static void method1() {
        for (int i=0; i<3; i++) {
            System.out.println("method01,  Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized  void method2() {
        for (int i=0; i<3; i++) {
            System.out.println("method02, Thread name: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

输出:
method01, Thread name: A
method02, Thread name: B
method01, Thread name: A
method02, Thread name: B
method01, Thread name: A
method02, Thread name: B
从输出可以看出,类锁和对象锁之间是互不干扰的,没什么影响。

如有问题,忘大家指正。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值