关于锁的对象

锁的对象共有两种:类对象和Class模板对象

1.标准情况下,两个线程、一个对象,先打印发短信还是打电话?发短信
2.发短信方法设置延迟4秒后,两个线程、一个对象,先打印发短信还是打电话?发短信
Synchronized锁的对象是方法的调用者,故两个方法用的是同一把锁,谁先拿到谁执行

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test01 {
    public static void main(String[] args) {
        Phone phone = new Phone();
        new Thread(()->{
            phone.sendMsg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

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

//资源类
class Phone{
    public synchronized void sendMsg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"发短信");
    }
    public synchronized void call(){
        System.out.println(Thread.currentThread().getName()+"打电话");
    }
}

3.两个线程、一个对象、一个同步方法、一个普通方法,先打印发短信还是hello?hello
因为发短信方法会延迟4秒,hello是普通方法,无锁,不是同步方法,不受锁的影响,不存在抢占资源情况。

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test02 {
    public static void main(String[] args) {
        Phone2 phone = new Phone2();
        new Thread(()->{
            phone.sendMsg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

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

//资源类
class Phone2{
    public synchronized void sendMsg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"发短信");
    }
    public synchronized void call(){
        
        System.out.println(Thread.currentThread().getName()+"打电话");
    }

    //普通方法
    public void hello(){
        System.out.println(Thread.currentThread().getName()+"=>hello");
    }
}

4.两个线程、两个对象、两个同步方法,先打印发短信还是打电话?打电话
两个对象会存在两把锁,锁不一样,故按照时间顺序来执行

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test03 {
    public static void main(String[] args) {
        Phone3 phone1 = new Phone3();
        Phone3 phone2 = new Phone3();
        new Thread(()->{
            phone1.sendMsg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

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

//资源类
class Phone3{
    public synchronized void sendMsg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"发短信");
    }
    public synchronized void call(){

        System.out.println(Thread.currentThread().getName()+"打电话");
    }

    //普通方法
    public void hello(){
        System.out.println(Thread.currentThread().getName()+"=>hello");
    }
}

5.两个线程、一个对象、两个静态同步方法,先打印发短信还是打电话?发短信
当同步方法为静态时,锁的是其Class模板,一个类只有一个Class对象

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test04 {
    public static void main(String[] args) {
        Phone4 phone4 = new Phone4();
        new Thread(()->{
            phone4.msg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            phone4.call();
        },"B").start();
    }
}
class Phone4{
    public static synchronized void msg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    public static synchronized void call(){
        System.out.println("打电话");
    }
}

6.两个线程、两个对象、两个静态同步方法,先打印发短信还是打电话?发短信
当同步方法为静态时,锁的是其Class模板,一个类只有唯一的一个Class对象

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test04 {
    public static void main(String[] args) {
        Phone4 phone1 = new Phone4();
        Phone4 phone2 = new Phone4();

        new Thread(()->{
            phone1.msg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            phone2.call();
        },"B").start();
    }
}
class Phone4{
    public static synchronized void msg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    public static synchronized void call(){
        System.out.println("打电话");
    }
}

7.两个线程、一个对象、一个静态同步方法、一个普通同步方法,先打印发短信还是打电话?打电话
静态同步方法锁的是Class类对象,普通同步方法锁的是类对象,因此拥有两把锁,互不冲突

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test05 {
    public static void main(String[] args) {
        Phone5 phone = new Phone5();

        new Thread(()->{
            phone.msg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            phone.call();
        },"B").start();
    }
}
class Phone5{
    public static synchronized void msg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}

8.两个线程、两个对象、一个静态同步方法、一个普通同步方法,先打印发短信还是打电话?打电话
拥有两个锁,故先输出没有延时的

package com.itheima.lock8;

import java.util.concurrent.TimeUnit;

public class Test05 {
    public static void main(String[] args) {
        Phone5 phone1 = new Phone5();
        Phone5 phone2 = new Phone5();

        new Thread(()->{
            phone1.msg();
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            phone2.call();
        },"B").start();
    }
}
class Phone5{
    public static synchronized void msg(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值