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();
    }
    
  • 相关阅读:
    C#如何给Listbox添加右键菜单
    [GraphQL] Query Lists of Multiple Types using a Union in GraphQL
    [GraphQL] Query GraphQL Interface Types in GraphQL Playground
    [GraphQL] Reuse GraphQL Selection Sets with Fragments
    [Dart] final vs const
    [GraphQL] Set variable and default value & alias
    [置顶] 【Git入门之十一】标签管理
    poj 3182 The Grove bfs
    hdu1166敌兵布阵
    <WinForm_1>初识WinForm编程
  • 原文地址:https://www.cnblogs.com/dalianpai/p/14205378.html
Copyright © 2011-2022 走看看