栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java中的线程八锁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java中的线程八锁

public class Main {
    public synchronized void test(){
    }
}
等价于
public class Main {
    public  void test(){
        synchronized(this){
        }
    }
}
public class Main {
    public static synchronized void test(){
    }
}
等价于
public class Main {
    public static  void test(){
        synchronized (Main.class){
        }
    }
}
结论:
  • 非静态同步方法锁住的是实例对象本身
  • 静态同步方法锁住的是类对象本身

线程八锁------>考察的就是锁住哪个对象

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


线程t1和线程t2锁住的是同一个实例对象n1,有竞争
情况:ab或者ba

@Slf4j(topic = "c.Number")
public class Number {
    public synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public synchronized void b(){
        log.debug("b");
    }
    public static void main(String[] args) {
        Number n1=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n1.b(),"t2").start();
    }
}
线程t1和线程t2锁住的是同一个实例对象n1,有竞争
情况:1s后ab或者 b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
    public synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public synchronized void b(){
        log.debug("b");
    }
    public void c(){
        log.debug("c");
    }
    public static void main(String[] args) {
        Number n1=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n1.b(),"t2").start();
        new Thread(()->n1.c(),"t3").start();
    }
}
线程t3没有去争抢锁
情况:c 1s 后ab 或者 bc 1s 后 a 或者 cb 1s后a

@Slf4j(topic = "c.Number")
public class Number {
    public synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public synchronized void b(){
        log.debug("b");
    }

    public static void main(String[] args) {
        Number n1=new Number();
        Number n2=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n2.b(),"t2").start();

    }
}
线程t1和线程t2锁住的是不同的实例对象,没有竞争
情况 b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public synchronized void b(){
        log.debug("b");
    }

    public static void main(String[] args) {
        Number n1=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n1.b(),"t2").start();

    }
}
线程t1锁住的是类对象本身,线程t2锁住的是n1实例对象本身,没有竞争
情况: b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public static synchronized void b(){
        log.debug("b");
    }

    public static void main(String[] args) {
        Number n1=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n1.b(),"t2").start();

    }
}
线程t1和线程t2锁住的都是类对象本身,有竞争
情况: 1s 后 ab  或者 b 1s后 a
@Slf4j(topic = "c.Number")
public class Number {
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public  synchronized void b(){
        log.debug("b");
    }

    public static void main(String[] args) {
        Number n1=new Number();
        Number n2=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n2.b(),"t2").start();

    }
}
线程t1锁住的是类对象本身,线程t2锁住的是n2实例对象本身,没有竞争
情况 b 1s 后 a
@Slf4j(topic = "c.Number")
public class Number {
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        log.debug("a");
    }
    public static synchronized void b(){
        log.debug("b");
    }

    public static void main(String[] args) {
        Number n1=new Number();
        Number n2=new Number();
        new Thread(()-> {
            try {
                n1.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->n2.b(),"t2").start();

    }
}
线程t1和t2锁住的都是类对象本身,有竞争
情况: 1s后ab 或者 b 1s后 a

有误请指正
参考:黑马程序员全面深入学习Java并发编程,JUC并发编程全套教程.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591112.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号