synchronized到底锁的是谁?

synchronized实现线程同步,让多个线程排队以此获取某个资源,保证数据不会出错。

synchronized到底锁定的是什么元素?

修饰方法

  1. 静态方法(锁定的是类

  2. 非静态方法锁定的是方法的调用者

import java.util.concurrent.TimeUnit;
public class Test {
    public static void main(String[] args) {
        Data data = new Data();
        //线程A
        new Thread(()->{
            data.func1();
        },"A").start();


        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //休眠一秒后启动线程B
        new Thread(()->{
            data.func2();
        },"B").start();
    }
}

class Data{
    public  void func1(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1....");
    }
    public   void func2(){
        System.out.println("2....");
    }
}

不加锁:一秒之后执行B线程,再等两秒指向A线程

class Data{
    public synchronized void func1(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1....");
    }
    public synchronized  void func2(){
        System.out.println("2....");
    }
}
  • 加锁之后,等3秒后执行A,B线程同时输出
  • A,B线程同时争夺,data是调用者被锁住,data只能依次执行
  • 若func2不加上synchronized,那么A,B不会竞争
public class Test {
    public static void main(String[] args) {
        Data data = new Data();
        //线程A
        new Thread(()->{
            data.func1();
        },"A").start();


        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //休眠一秒后启动线程B
        new Thread(()->{
            data.func2();
        },"B").start();
    }
}

class Data{
    public synchronized static void func1(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1....");
    }
    public synchronized static   void func2(){
        System.out.println("2....");
    }
}

A.B会排队

import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        Data data1 = new Data();
        Data data2 = new Data();
        //线程A
        new Thread(()->{
            data1.func1();
        },"A").start();


        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //休眠一秒后启动线程B
        new Thread(()->{
            data2.func2();
        },"B").start();
    }
}

class Data{
    public synchronized static void func1(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1....");
    }
    public synchronized static   void func2(){
        System.out.println("2....");
    }
}

不同对象调用,AB仍然会排队

修饰代码块

锁定的是传入的对象

import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        Data2 data2 = new Data2();
        for(int i = 0; i < 5; i++){
            new Thread(()->{
                data2.func();
            }).start();
        }
    }
}

class Data2{
    public void func(){
        System.out.println("start...");
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end...");
    }
}

在这里插入图片描述


import java.util.concurrent.TimeUnit;


public class Test {
    public static void main(String[] args) {
        Data2 data2 = new Data2();
        for(int i = 0; i < 5; i++){
            new Thread(()->{
                data2.func();
            }).start();
        }
    }
}

class Data2{
    public void func(){
        synchronized (this){ // 争夺资源,调用的对象
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end...");
        }

    }
}

排队争夺data2对象

在这里插入图片描述


        for(int i = 0; i < 5; i++){
            new Thread(()->{
                new Data2().func();
            }).start();
        }

这样就不会争夺了,不用排队


public class Test {
    public static void main(String[] args) {
        for(int i = 0; i < 5; i++){
            new Thread(()->{
                new Data2().func();
            }).start();
        }
    }
}

class Data2{
    public void func(){
        synchronized (Data2.class){ // 争夺资源
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end...");
        }

    }
}

锁Data2运行时类,会排队


public class Test {
    public static void main(String[] args) {
        for(int i = 0; i < 5; i++){
            new Thread(()->{
                new Data2().func();
            }).start();
        }
    }
}

class Data2{
    public void func(){
        Integer num = 1;
        synchronized (num){ // 争夺资源
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end...");
        }

    }
}

仍然排队,说明num在内存中只有一份

还有其他的题目,关键是看争夺的对象在内存中有几份

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值