线程安全性(一) 原子性 基于锁的实现 - synchronized修饰代码块

锁概述

锁是Java中除了AtomicXXX类之外,另一种实现原子性的方式,典型的2中基于锁实现原子性的方式是synchronized关键字和基于Lock接口的实现类;

synchronized关键字加锁
  • 依赖于JVM的锁实现有synchronized关键字加锁;
  • synchronized关键字加的锁是不可中断锁,适合竞争不激烈,可读性好;
  • synchronized关键字加的锁在竞争激烈时性能下降的非常快;
Lock接口的实现类
  • 依赖于特殊的CPU指令,比如JDK中的java.util.concurrent.locks.Lock接口,比较有代表性的实现类有ReentrantLock;
  • Lock接口的实现类加的锁是可中断锁,竞争激烈时能维持常态;

synchronized能修饰的四种对象

  • 修饰代码块:被修饰的代码称作同步语句块,其作用范围是大括号括起来的代码,作用于调用的对象;如果代码块就是整个方法体,那么和修饰方式是等同的;
  • 修饰方法:被修饰的方法称为同步方法,作用范围是整个方法,作用于调用的对象;
  • 修饰静态方法:作用范围是整个静态方法,作用于这个类的所有对象;
  • 修饰类:作用范围是synchronized后面的括号括起来的部分,作用于这个类的所有对象;

synchronized修饰代码块示例 - 正例

  • 同一个对象不同线程中访问同步代码块,同步代码块对同一个调用对象是起到同步作用的;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedCodeBlock {

    public void test1(int j) {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                log.info("synchronized block object {} - {}", j, i);
            }
        }
    }

    public static void main(String[] args) {
        testCodeBlockSync();
    }

    private static void testCodeBlockSync() {
        SynchronizedCodeBlock example1 = new SynchronizedCodeBlock();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(() -> {
            example1.test1(1);
        });
        service.execute(() -> {
            example1.test1(1);
        });
    }

}

输出:

  • 同一个对象所在的2个线程按顺序先后执行;

17:06:51.597 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 0
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 1
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 2
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 3
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 4
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 5
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 6
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 7
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 8
17:06:51.602 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 9
17:06:51.602 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 0
17:06:51.602 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 1
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 2
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 3
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 4
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 5
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 6
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 7
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 8
17:06:51.603 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 9

synchronized修饰代码块示例 - 反例

  • 不同对象不同线程中访问同步代码块,同步代码块对不同调用对象是不能起到同步作用的;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedCodeBlock {

    public void test1(int j) {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                log.info("synchronized block object {} - {}", j, i);
            }
        }
    }

    public static void main(String[] args) {
        testCodeBlockNotSync();
    }

    private static void testCodeBlockNotSync() {
        SynchronizedCodeBlock example1 = new SynchronizedCodeBlock();
        SynchronizedCodeBlock example2 = new SynchronizedCodeBlock();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(() -> {
            example1.test1(1);
        });
        service.execute(() -> {
            example2.test1(2);
        });
    }

}

输出:

  • 乱序的,两个对象所在的线程同时执行;

17:07:52.278 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 0
17:07:52.278 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 0
17:07:52.284 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 1
17:07:52.284 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 1
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 2
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 2
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 3
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 3
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 4
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 4
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 5
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 6
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 7
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 5
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 8
17:07:52.285 [pool-1-thread-1] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 1 - 9
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 6
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 7
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 8
17:07:52.285 [pool-1-thread-2] INFO com.example.concurrency.example.SynchronizedCodeBlock - synchronized block object 2 - 9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值