有关锁的8个问题

1.第1段代码

package com.zmh.multithread.juc;

class Phone {

    public synchronized void sendSMS() throws Exception {
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail");
    }
}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
        Thread.sleep(100);
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"BB").start();
    }
}

 输出结果:

------sendSMS
------sendEmail

原因:如果不加Thread.sleep(100)就不能确定谁先谁后,非静态同步方法的锁是当前对象this,一个类中的所有非静态方法都是同一个锁this,同一时刻只能有一个线程进入同步方法。Thread.sleep()写在哪里就阻塞在哪里,这里写在了main线程里,所以main线程下面的线程BB被阻塞,只能先执行线程AA。

第2段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }
}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone = new Phone();
      new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

------sendSMS
------sendEmail

原因:sleep方法没有释放锁,睡醒了以后接着从以前的上下文的位置继续运行,所以还是先输出------sendSMS。

第3段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }

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

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone = new Phone();
      new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone.hello();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

hello world
------sendSMS    AA

原因:hello方法没有加锁线程可以访问。

第4段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }

}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
      new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone2.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

------sendEmail    BB
------sendSMS    AA

原因:非静态同步方法的锁是当前对象this,AA线程的锁对象是phone1,BB线程的锁对象是phone2。不是同一个锁,两个线程之间都锁不住对方。

第5段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public static synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public static synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }

}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone= new Phone();
      new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

------sendSMS    AA
------sendEmail    BB

原因:静态同步方法的锁对象是当前类.Class对象,两个线程都是同一个类的对象(同一个对象)。即同一个类的同一个对象。是同一个锁。

第6段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public static synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public static synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }

}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone1= new Phone();
        Phone phone2= new Phone();
      new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone2.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

------sendSMS    AA
------sendEmail    BB

原因:静态同步方法的锁对象是当前类.Class对象,两个线程的锁对象都是同一个类的对象(不同对象)。即同一个类的不同对象,但终归属于同一个类,是同一个锁。

第7段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public static synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }

}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone= new Phone();
      new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

------sendEmail    BB
------sendSMS    AA

原因:静态同步方法的锁对象是当前类.Class对象,非静态同步方法的锁对象是当前对象this,不是同一个锁,相互之间没有影响。好比this是厕所的门,类锁是学校的教室的锁,你锁住了教室,厕所是可以进的,你锁住厕所,教室是可以进的。

第8段代码

package com.zmh.multithread.juc;

import java.util.concurrent.TimeUnit;

class Phone {

    public static synchronized void sendSMS() throws Exception {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
    }
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
    }

}

/**
 *
 * @Description: 8锁
 * @author gakkimarryme
 *
 */
public class Lock_8 {
    public static void main(String[] args) throws Exception{
        Phone phone1= new Phone();
        Phone phone2= new Phone();
      new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();
      Thread.sleep(100);

     new Thread(()->{
        try {
            phone2.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"BB").start();
    }
}

 输出结果:

------sendEmail    BB
------sendSMS    AA

原因:同上,也不是同一把锁。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值