JUC-8锁问题

JUC-8锁问题

1.两个synchronized方法的调用

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone1 ::call).start();
    }
}

class Phone1{
    public synchronized void sendmsg(){

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

先输出 发短信 再输出 打电话

2.两个synchronized方法的调用(其中第一个延迟4s)

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone1 ::call).start();
    }
}

class Phone1{
    public synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}

先输出 发短信 再输出 打电话

这说明,不是因为发短信这个线程先执行,所以先打印;而是因为synchronized锁的存在,这个锁 锁的是对象 即 phone1

3.一个synchronized方法、一个普通方法的调用

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test4 {
    public static void main(String[] args) {
        Phone3 phone1 = new Phone3();
        new Thread(phone1::sendmsg).start();

        new Thread(phone1::hello).start();
    }
}

class Phone3{
    public synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }

    public void hello(){
        System.out.println("hello");
    }
}

先输出 hello 再输出 发短信
原因:hello()方法是一个普通方法,没有synchronized关键字,不会被锁控制

4.两个对象,分别进行synchronized方法的调用

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        Phone1 phone2 = new Phone1();
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone2::call).start();
    }
}

class Phone1{
    public synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}

先输出 打电话 再输出 发短信
原因:synchronized锁的东西是对象,现在有两个对象,所以不会相互影响,call()不用等待4s,所以先输出

5.两个静态synchronized方法的调用(其中第一个延迟4s)

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone1 ::call).start();
    }
}

class Phone1{
    public static synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public static synchronized void call(){
        System.out.println("打电话");
    }
}

先输出 发短信 再输出 打电话

6.两个对象,分别进行静态synchronized方法的调用

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        Phone1 phone2 = new Phone1();
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone2::call).start();
    }
}

class Phone1{
    public static synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public static synchronized void call(){
        System.out.println("打电话");
    }
}

先输出 发短信 再输出 打电话
原因:从 5,6可以看出,static 修饰 synchronized后,由于static修饰的方法在编译时就加载完成,所以此时synchronized锁的对象时Phone1.class 一个class类只会有一个.class,所以即使是两个对象也没用,因为锁的是底层的类

7.一个静态synchronized方法、一个synchronized方法的调用

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
       
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone1 ::call).start();
    }
}

class Phone1{
    public static synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}

先输出 打电话 再输出 发短信
原因:因为static synchronized锁的是.class synchronized锁的是对象 phone1,是两把不同的锁,互不干扰

8.两个对象,一个对象静态synchronized方法、一个对象synchronized方法的调用

package com.kuang.lock8;

import java.util.concurrent.TimeUnit;

public class Test3 {
    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        Phone1 phone2 = new Phone1();
        new Thread(phone1::sendmsg).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(phone2::call).start();
    }
}

class Phone1{
    public static synchronized void sendmsg(){

        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}

先输出 打电话 再输出 发短信 原因同7

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值