线程8锁问题

1.标准访问:先发送邮件后发送短信

2.暂停3s:先发送邮件后发送短信

public class ThreadDemo5 {
    
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();
        
        Thread.sleep(1000);

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

    }
}
/* 一个对象里面如果有多个synchronized方法,某一时刻内,只要一个线程调用其中一个synchronized方法,
*  其他线程只能等待。换句话说,某一时刻只能有唯一一个线程去访问这些synchronized方法。
* 
*  锁的是当前对象this,被锁定后,其他线程不能进入到当前对象的其他synchronized方法。
*/
class Phone{

    public synchronized void sendMessage(){
        System.out.println("sendMsg");
    }
    public synchronized void sendEmail() throws InterruptedException {
    	//第二种场景
        TimeUnit.SECONDS.sleep(3);
        System.out.println("sendEmail");
    }
}

3.新增普通方法sendQQ():先发送QQ后发送邮件

public class ThreadDemo5 {
    //先发送短信后发送邮件
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        Thread.sleep(1000);

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

    }
}

class Phone{

    public synchronized void sendMessage(){
        System.out.println("sendMsg");
    }
    public synchronized void sendEmail() throws InterruptedException {
        
        TimeUnit.SECONDS.sleep(3);

        System.out.println("sendEmail");
    }

    //第三种场景
    public void sendQQ(){
        System.out.println("sendQQ");
    }
}

4.两个phone:先发送短信后发送邮件

public class ThreadDemo6 {
    public static void main(String[] args) throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        Thread.sleep(1000);

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

5.两个静态同步方法,同一部手机:先发送邮件后发送短信

public class ThreadDemo5 {

    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        Thread.sleep(1000);

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

class Phone{

    public static synchronized void sendMessage(){
        System.out.println("sendMsg");
    }
    public static synchronized void sendEmail() throws InterruptedException {

        TimeUnit.SECONDS.sleep(3);
        System.out.println("sendEmail");
    }
}

6.两个静态同步方法,两部手机:先发送邮件后发送短信

public class ThreadDemo5 {

    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        Thread.sleep(1000);

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

class Phone{

    public static synchronized void sendMessage(){
        System.out.println("sendMsg");
    }
    public static synchronized void sendEmail() throws InterruptedException {

        TimeUnit.SECONDS.sleep(3);
        System.out.println("sendEmail");
    }
}

7.一个静态同步方法,一个普通同步方法,同一部手机:先发送短信后发送邮件

public class ThreadDemo5 {

    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        Thread.sleep(1000);

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

class Phone{

    public synchronized void sendMessage(){
        System.out.println("sendMsg");
    }
    public static synchronized void sendEmail() throws InterruptedException {

        TimeUnit.SECONDS.sleep(3);
        System.out.println("sendEmail");
    }
}

8.一个静态同步方法,一个普通同步方法,两部手机:先发送短信后发送邮件

public class ThreadDemo5 {

    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        Thread.sleep(1000);

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

class Phone{

    public synchronized void sendMessage(){
        System.out.println("sendMsg");
    }
    public static synchronized void sendEmail() throws InterruptedException {

        TimeUnit.SECONDS.sleep(3);
        System.out.println("sendEmail");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值