3.锁现象详解

1、锁现象

什么是锁!如何判断锁的是谁!

1.1、都是synchronized方法
/*
* 结果:发短信    打电话
* */
public class Test1 {
    public static void main(String[] args) {
        Phone1 phone = new Phone1();
        new Thread(() -> {
            phone.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone.call();
        }, "b").start();
    }
}

class Phone1 {
    /*
     * synchronized 锁的对象是方法调用者(phone)
     * 两个方法用的是同一个锁(phone),谁先拿到谁执行
     * */
    public synchronized void sendMsg() {
        System.out.println("发短信");
    }

    public synchronized void call() {
        System.out.println("打电话");
    }
}

总结:synchronized 锁的对象是方法调用者(phone),两个方法用的是同一个锁(phone),谁先拿到谁执行

1.2、sendMsg延迟3秒

结果不变:发短信 打电话

//main方法同上
class Phone1 {
    /*
     * synchronized 锁的对象是方法调用者(phone)
     * 两个方法用的是同一个锁(phone),谁先拿到谁执行
     * */
    public synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public synchronized void call() {
        System.out.println("打电话");
    }
}

总结:synchronized锁的对象是方法调用者(phone),两个方法用的是同一个锁(phone),谁先拿到谁执行

1.3、同步和普通方法都有
/*
 * 结果:hello 发短信
 * */
public class Test3 {
    public static void main(String[] args) {
        Phone3 phone = new Phone3();
        new Thread(() -> {
            phone.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone.hello();
        }, "b").start();
    }
}

class Phone3 {
    public synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    //这里没有锁!不是同步方法,不受锁的影响
    public void hello(){
        System.out.println("hello");
    }
}

总结:普通方法不受锁的影响

1.4、多个对象,只有同步方法
/*
 * 结果:打电话 发短信
 * */
public class Test4 {
    public static void main(String[] args) {
        //两个对象,两个调用者,两把锁
        Phone4 phone1 = new Phone4();
        Phone4 phone2 = new Phone4();
        new Thread(() -> {
            phone1.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone2.call();
        }, "b").start();
    }
}

class Phone4 {
    public synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public synchronized void call() {
        System.out.println("打电话");
    }
}

总结:多个资源对象,就是多个锁

1.5、单个对象,只有静态同步方法
/*
 * 结果:发短信   打电话
 * */
public class Test5 {
    public static void main(String[] args) {
        //两个对象,两个调用者,两把锁
        Phone5 phone = new Phone5();
        new Thread(() -> {
            phone.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone.call();
        }, "b").start();
    }
}

//只有唯一的一个Class对象 Phone5.class
//sendMsg和call方法都被static修饰了,是同一个锁
class Phone5 {
    /*
    * static方法,类一加载就有,锁的是Class 模板
    * */
    public static synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public static synchronized void call() {
        System.out.println("打电话");
    }
}

总结:static方法类初始化时加载,锁的是Class对象,是一把锁

1.6、多个对象,只有静态同步方法
/*
 * 结果:发短信   打电话
 * */
public class Test6 {
    public static void main(String[] args) {
        //两个对象的Class类模板只有一个,锁的是Class
        Phone6 phone1 = new Phone6();
        Phone6 phone2 = new Phone6();
        new Thread(() -> {
            phone1.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone2.call();
        }, "b").start();
    }
}

class Phone6 {
    /*
     * static方法,类一加载就有,锁的是Class 模板
     * */
    public static synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public static synchronized void call() {
        System.out.println("打电话");
    }
}
1.7、单个对象,静态同步方法和普通同步方法都有
/*
 * 结果:打电话   发短信
 * */
public class Test7 {
    public static void main(String[] args) {
        Phone7 phone = new Phone7();
        new Thread(() -> {
            phone.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone.call();
        }, "b").start();
    }
}

class Phone7 {
    //静态同步方法,锁的是Class类模板
    public static synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    //普通同步方法,锁的调用者
    public synchronized void call() {
        System.out.println("打电话");
    }
}

总结:静态同步方法,锁的是Class类模板,普通同步方法,锁的调用者,是不同的锁

1.8、多个对象,静态同步方法和普通同步方法都有
/*
 * 结果:打电话   发短信
 * */
public class Test8 {
    public static void main(String[] args) {
        Phone8 phone1= new Phone8();
        Phone8 phone2= new Phone8();
        new Thread(() -> {
            phone1.sendMsg();
        }, "a").start();

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

        new Thread(() -> {
            phone2.call();
        }, "b").start();
    }
}

class Phone8 {
    //静态同步方法,锁的是Class类模板
    public static synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    //普通同步方法,锁的调用者
    public synchronized void call() {
        System.out.println("打电话");
    }
}

总结:静态同步方法,锁的是Class类模板,普通同步方法,锁的调用者,是不同的锁

1.9、结论
  • new this 具体的一个手机
  • stataic Clas 唯一的一个模板
posted @   jpy  阅读(4)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示