通过线程八锁问题融会贯通synchronized关键字的使用


线程八锁


timg

凯有八门遁甲之术,你晓得线程八锁问题吗?赶紧来看一下吧~


1. 一锁
class Number{
    public synchronized void a() {
        log.debug("1");
    }
    public synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }, "t1").start();
    new Thread(()->{ n1.b(); }, "t2").start();
}

执行主方法,控制台的输出可能是2 1或是1 2。因为创建的两个线程在主方法中创建并启动,它们之间谁先能拿到CPU的使用权就谁先执行:

  • 如果t1先分到时间片,那么输出为1 2
  • 如果t2先分到时间片,那么输出为2 1

2. 二锁
class Number{
    public synchronized void a() {
        sleep(1);  // 需先睡眠1s
        log.debug("1");
    }
    public synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }, "t1").start();
    new Thread(()->{ n1.b(); }, "t2").start();
}

情况和上面类似,可能的结果有:

  • 如果t1先分到时间片,那么输出情况为:等待1s输出1 2
  • 如果t2先分到时间片,那么输出情况为:先输出2,等待1s后输出1

3. 三锁
class Number{
    // a、b执行需要先获取锁
    public synchronized void a() {
        sleep(1);
        log.debug("1");
    }
    
    public synchronized void b() {
        log.debug("2");
    }
    
    // c执行不需要获取锁
    public void c() {
        log.debug("3");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }, "t1").start();
    new Thread(()->{ n1.b(); }, "t2").start();
    new Thread(()->{ n1.c(); }, "t3").start();
}

此时主方法中存在三个线程,c无需获取锁,它会和t1、t2中先获得锁的那个线程同时输出,输出的前后顺序不固定,后续的分析就和上面的一样了。


4. 四锁
class Number{
    public synchronized void a() {
        sleep(1);
        log.debug("1");
    }
    public synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    Number n2 = new Number();
    new Thread(()->{ n1.a(); }, "t1").start();
    new Thread(()->{ n2.b(); }, "t2").start();
}

此时t1和t2两个线程之间不存在锁竞争问题,所以它们可以同时获得各自的锁,但是由于t1需要先睡眠1s,所以先输出为2,等待1s后输出1。


5. 五锁
class Number{
    // 锁对象为Number.Class
    public static synchronized void a() {
        sleep(1);
        log.debug("1");
    }
    
    // 锁对象为this对象
    public synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }, "t1").start();
    new Thread(()->{ n2.b(); }, "t2").start();
}

t1和t2两个线程之间仍然不存在线程竞争问题,所以依然先输出为2,等待1s后输出1。


6. 六锁
class Number{
    // 锁对象为Number.Class
    public static synchronized void a() {
        sleep(1);
        log.debug("1");
    }
    
    // 锁对象为Number.Class
    public static synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }, "t1").start();
    new Thread(()->{ n2.b(); }, "t2").start();
}

t1和t2线程竞争的都是Number.Class这个对象锁,所以输出情况了一锁相同:

  • 先输出2,等待1s后输出1
  • 等待1s后输出1,然后输出2

7. 七锁
class Number{
     // 锁对象为Number.Class
    public static synchronized void a() {
        sleep(1);
        log.debug("1");
    }
    
    // 锁对象为this对象
    public synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    Number n2 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n2.b(); }).start();
}

此时t1和t2两个线程之间不存在锁竞争问题,所以它们可以同时获得各自的锁,但是由于t1需要先睡眠1s,所以先输出为2,等待1s后输出1。


8. 八锁
class Number{
    // 锁对象为Number.Class
    public static synchronized void a() {
        sleep(1);
        log.debug("1");
    }
    
    // 锁对象为Number.Class
    public static synchronized void b() {
        log.debug("2");
    }
}
public static void main(String[] args) {
    Number n1 = new Number();
    Number n2 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n2.b(); }).start();
}

此时,t1和t2线程通过不同的Number对象启动,但是它们竞争的仍然都是Number.Class这个类对象锁,所以输出情况了一锁、六锁相同:

  • 先输出2,等待1s后输出1
  • 等待1s后输出1,然后输出2

看完了线程八锁问题,你应该对于synchronized 对象的锁问题有了清晰的理解与使用了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值