synchronized关键字(二):案例分析

前言

synchronized系列文章
synchronized关键字(一):实现原理

学完前一篇:synchronized关键字(一):实现原理 之后,我们对synchronized有了深入的理解,理解之后肯定就是运用了。所以本篇主要讲解synchronized关键字的几种实际应用场景,总结了一下,synchronized关键字的主要使用场景,从易到难,大致分为以下几种情况

案例

两个线程同时访问同一个对象的非静态同步方法

public class SynchronizedDemo1 implements Runnable {

    public static void main(String[] args) {
    	// 同一个对象
        SynchronizedDemo1 lock = new SynchronizedDemo1();
        Thread thread1 = new Thread(lock, "thread-1");
        Thread thread2 = new Thread(lock, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        display();
    }

    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

执行结果

thread-1 -> 1569765814781 -> display()
thread-1 -> 1569765816781 -> finished
thread-2 -> 1569765816781 -> display()
thread-2 -> 1569765818782 -> finished

注意输出时间,thread-1输出display()后休眠了2秒,输出finished,马上thread-2输出display(),休眠2秒后输出finished。说明两个线程同时访问同一个对象的非静态同步方法时,同步方法是串行的,即synchronized锁有效

两个线程同时访问两个对象的非静态同步方法

public class SynchronizedDemo2 implements Runnable {

    public static void main(String[] args) {
        // 不同对象
        SynchronizedDemo2 lock1 = new SynchronizedDemo2();
        SynchronizedDemo2 lock2 = new SynchronizedDemo2();
        Thread thread1 = new Thread(lock1, "thread-1");
        Thread thread2 = new Thread(lock2, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        display();
    }

    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569766073360 -> display()
thread-2 -> 1569766073361 -> display()
thread-1 -> 1569766075362 -> finished
thread-2 -> 1569766075362 -> finished

从输出结果可以看出,thread-1输出display()后,thread-2马上输出display(),然后各自休眠2秒,输出finished。可以说明两个线程同时访问两个对象的同步方法时,代码不是串行的,即synchronized锁无效(使用方式错误)

两个线程同时访问synchronized静态方法

public class SynchronizedDemo3 implements Runnable {

    public static void main(String[] args) {
        // 不同对象
        SynchronizedDemo3 lock1 = new SynchronizedDemo3();
        SynchronizedDemo3 lock2 = new SynchronizedDemo3();
        Thread thread1 = new Thread(lock1, "thread-1");
        Thread thread2 = new Thread(lock2, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        display();
    }
    
    // 静态方法
    private static synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569766374886 -> display()
thread-1 -> 1569766376887 -> finished
thread-2 -> 1569766376888 -> display()
thread-2 -> 1569766378888 -> finished

thread-1输出display()后休眠了2秒,输出finished,马上thread-2输出display(),休眠2秒后输出finished。说明两个线程同时访问synchronized静态方法时,同步方法是串行的(即使传入两个不同的对象),即synchronized锁有效

两个线程同时访问非静态同步方法与普通方法

public class SynchronizedDemo4 implements Runnable {

    public static void main(String[] args) {
        SynchronizedDemo4 lock1 = new SynchronizedDemo4();
        SynchronizedDemo4 lock2 = new SynchronizedDemo4();
        Thread thread1 = new Thread(lock1, "thread-1");
        Thread thread2 = new Thread(lock2, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        if (Objects.equals(Thread.currentThread().getName(), "thread-1")) {
            // thread-1访问非静态同步方法
            display();
        } else {
            // thread-2访问普通方法
            show();
        }
    }

    // 非静态同步方法
    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }

    // 普通方法
    private void show() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> show()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569766853881 -> display()
thread-2 -> 1569766853882 -> show()
thread-1 -> 1569766855883 -> finished
thread-2 -> 1569766855883 -> finished

thread-1输出非静态同步方法display()后,thread-2马上输出了普通方法中的show(),然后各自休眠2秒后输出finished。说明两个线程同时访问非静态同步方法与普通方法时,非静态同步方法与普通方法不是串行的,即synchronized锁无效

两个线程同时访问同一个对象的不同非静态synchronized方法

public class SynchronizedDemo5 implements Runnable {

    public static void main(String[] args) {
        // 同一个对象
        SynchronizedDemo5 lock = new SynchronizedDemo5();
        Thread thread1 = new Thread(lock, "thread-1");
        Thread thread2 = new Thread(lock, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        if (Objects.equals(Thread.currentThread().getName(), "thread-1")) {
            // thread-1访问非静态同步方法
            display();
        } else {
            // thread-2访问非静态同步方法
            show();
        }
    }

    // 非静态同步方法
    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }

    // 非静态同步方法
    private synchronized void show() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> show()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569767264895 -> display()
thread-1 -> 1569767266896 -> finished
thread-2 -> 1569767266896 -> show()
thread-2 -> 1569767268897 -> finished

thread-1输出display()后休眠了2秒,输出finished,马上thread-2输出show(),休眠2秒后输出finished。说明两个线程同时访问同一个对象的不同非静态synchronized方法,同步方法是串行的,即synchronized锁有效

两个线程同时访问静态synchronized方法和非静态synchronized方法

public class SynchronizedDemo6 implements Runnable {

    public static void main(String[] args) {
        SynchronizedDemo6 lock = new SynchronizedDemo6();
        Thread thread1 = new Thread(lock, "thread-1");
        Thread thread2 = new Thread(lock, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        if (Objects.equals(Thread.currentThread().getName(), "thread-1")) {
            // thread-1访问非静态同步方法
            display();
        } else {
            // thread-2访问静态同步方法
            show();
        }
    }

    // 非静态同步方法
    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }

    // 静态同步方法
    private static synchronized void show() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> show()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569767538794 -> display()
thread-2 -> 1569767538795 -> show()
thread-1 -> 1569767540796 -> finished
thread-2 -> 1569767540796 -> finished

thread-1输出非静态同步方法display()后,thread-2马上输出了静态同步方法中的show(),然后各自休眠2秒后输出finished。说明两个线程同时访问静态synchronized方法和非静态synchronized方法时,不是串行的,即synchronized锁无效

总结

以上几种情况,虽说看起来比较复杂,也比较绕。但是归根结底,只需要明确两点,就可以做到百战不殆。

  • 明确线程获取的是对象锁还是类锁
  • 同一把锁,在同一时刻最多被一个线程获取

上面六个案例,围绕这两点分析,相信会很好理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. synchronized关键字在使用层面的理解 synchronized关键字是Java中用来实现线程同步的关键字,可以修饰方法和代码块。当线程访问被synchronized修饰的方法或代码块时,需要获取对象的锁,如果该锁已被其他线程获取,则该线程会进入阻塞状态,直到获取到锁为止。synchronized关键字可以保证同一时刻只有一个线程能够访问被锁定的方法或代码块,从而避免了多线程并发访问时的数据竞争和一致性问题。 2. synchronized关键字在字节码中的体现 在Java代码编译成字节码后,synchronized关键字会被编译成monitorenter和monitorexit指令来实现。monitorenter指令对应获取锁操作,monitorexit指令对应释放锁操作。 3. synchronized关键字在JVM中的实现 在JVM中,每个对象都有一个监视器(monitor),用来实现对象锁。当一个线程获取对象锁后,就进入了对象的监视器中,其他线程只能等待该线程释放锁后再去竞争锁。 synchronized关键字的实现涉及到对象头中的标志位,包括锁标志位和重量级锁标志位等。当一个线程获取锁后,锁标志位被设置为1,其他线程再去获取锁时,会进入自旋等待或者阻塞等待状态,直到锁标志位被设置为0,即锁被释放后才能获取锁。 4. synchronized关键字在硬件方面的实现 在硬件层面,锁的实现需要通过CPU指令和总线锁来实现。当一个线程获取锁时,CPU会向总线发送一个锁请求信号,其他CPU收到该信号后会进入自旋等待状态,直到锁被释放后才能获取锁。总线锁可以保证多个CPU之间的原子操作,从而保证锁的正确性和一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值