synchronized到底锁的是谁

举例:


public class Data {

    public static void main(String[] args) {

        Test test=new Test();

        //线程A
        new Thread(()->{

            test.eat();
        },"A").start();


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

        //线程B
        new Thread(()->{

            test.run();
        },"B").start();

    }
}

class Test {

    public void eat(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("============1=eat");
    }


    public void run(){
        System.out.println("============2=run");
    }
}

结果:

============2=run
============1=eat

Process finished with exit code 0

再次修改:

public class Data {

    public static void main(String[] args) {

        Test test=new Test();

        //线程A
        new Thread(()->{

            test.eat();
        },"A").start();


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

        //线程B
        new Thread(()->{

            test.run();
        },"B").start();

    }
}

class Test {

    public synchronized void eat(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("============1=eat");
    }


    public synchronized void run(){
        System.out.println("============2=run");
    }
}

输出结果:非静态类:锁定的是方法的调用者

============1=eat
============2=run

Process finished with exit code 0

再次修改:去掉run方法的synchronized 关键字

public class Data {

    public static void main(String[] args) {

        Test test=new Test();

        //线程A
        new Thread(()->{

            test.eat();
        },"A").start();


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

        //线程B
        new Thread(()->{

            test.run();
        },"B").start();

    }
}

class Test {

    public synchronized void eat(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("============1=eat");
    }


    public void run(){
        System.out.println("============2=run");
    }
}

此时不需要争夺资源,A线程走完后 ,直接走run方法

结果:

============2=run
============1=eat

Process finished with exit code 0

再次修改,创建2个对象,不同对象调不同方法

public class Data {
    public static void main(String[] args) {
        Test test=new Test();
        Test test1=new Test();

        //线程A
        new Thread(()->{
            test.eat();
        },"A").start();


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

        //线程B
        new Thread(()->{
            test1.run();
        },"B").start();
    }
}

class Test {
    public synchronized void eat(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("============1=eat");
    }
    public synchronized void run(){
        System.out.println("============2=run");
    }
}

结果:

============2=run
============1=eat

Process finished with exit code 0

 

修饰静态方法示例:

public class Data {

    public static void main(String[] args) {

        Test test=new Test();
        Test test1=new Test();

        //线程A
        new Thread(()->{

            test.eat();
        },"A").start();
        
        try{
            TimeUnit.SECONDS.sleep(1);//睡眠1秒
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        //线程B
        new Thread(()->{

            test1.run();
        },"B").start();
    }
}

class Test {

    public synchronized static void eat(){
        try{
            TimeUnit.SECONDS.sleep(3);//睡眠3秒
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("============1=eat");
    }

    public synchronized static void run(){
        System.out.println("============2=run");
    }
}

结果:

============1=eat
============2=run

Process finished with exit code 0

此时需要等待 ,锁定的是Test类

再次修改:


public class Data {

    public static void main(String[] args) {

        Test test=new Test();
        Test test1=new Test();

        //线程A
        new Thread(()->{

            test.eat();
        },"A").start();

        try{
            TimeUnit.SECONDS.sleep(1);//睡眠1秒
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        //线程B
        new Thread(()->{

            test1.run();
        },"B").start();
    }
}

class Test {

    public synchronized static void eat(){
        try{
            TimeUnit.SECONDS.sleep(3);//睡眠3秒
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("============1=eat");
    }

    public static void run(){
        System.out.println("============2=run");
    }
}

结果:b线程调取方法没有 synchronized 不用锁定 直接执行就可以了

============2=run
============1=eat

Process finished with exit code 0
  • 修饰方法:

(1).静态方法 :锁定的是类
(2).非静态方法:锁定的是方法的调用者
  • 修饰代码块

锁定是传入的对象


 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我先来一碗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值