zoukankan      html  css  js  c++  java
  • 线程八锁

    所谓的“线程八锁”,其实就是考察 synchronized 锁住的是哪个对象
    情况1:12 或 21

    锁住的为同一对象,2个线程都有可能执行

    @Slf4j(topic = "c.Number")
    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(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况2:1s后12,或 2 1s后 1

    锁住的为同一对象,2个线程都有可能执行

    @Slf4j(topic = "c.Number")
    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();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况3:3 1s 12 或 23 1s 1 或 32 1s 1

    锁住的为同一对象,3个线程都有可能执行

    class Number{
    public synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public synchronized void b() {
    log.debug("2");
    }
    public void c() {
    log.debug("3");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    new Thread(()->{ n1.c(); }).start();
    }
    

    情况4:2 1s 后 1

    锁住的不为同一对象,不存在锁竞争,第二个线程先执行。

    @Slf4j(topic = "c.Number")
    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(); }).start();
    new Thread(()->{ n2.b(); }).start();
    }
    

    情况5:2 1s 后 1

    锁住的不为同一对象,不存在锁竞争,第二个线程先执行,第一个锁的是类,第二个是对象

    @Slf4j(topic = "c.Number")
    class Number{
    public static 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();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况6:1s 后12, 或 2 1s后 1

    锁住的为同一对象,2个线程都有可能执行

    @Slf4j(topic = "c.Number")
    class Number{
    public static synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    public static synchronized void b() {
    log.debug("2");
    }
    }
    public static void main(String[] args) {
    Number n1 = new Number();
    new Thread(()->{ n1.a(); }).start();
    new Thread(()->{ n1.b(); }).start();
    }
    

    情况7:2 1s 后 1

    锁住的不为同一对象,不存在锁竞争,第二个线程先执行

    @Slf4j(topic = "c.Number")
    class Number{
    public static 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(); }).start();
    new Thread(()->{ n2.b(); }).start();
    }
    

    情况8:1s 后12, 或 2 1s后 1

    锁住的为同一对象,2个线程都有可能执行

    class Number{
    public static synchronized void a() {
    sleep(1);
    log.debug("1");
    }
    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();
    }
    
  • 相关阅读:
    权限管理
    Linux常用命令
    SSM的开发步骤分析
    03每日课后作业面试题汇总
    Redis常用命令
    大觅网07day
    消息队列面试题
    bzoj 3745: [Coci2015]Norma
    Codeforces 343E Pumping Stations
    UOJ #236. 【IOI2016】railroad
  • 原文地址:https://www.cnblogs.com/dalianpai/p/14205378.html
Copyright © 2011-2022 走看看