美文网首页
多线程的问题和CAS

多线程的问题和CAS

作者: 叫我C30混凝土 | 来源:发表于2020-10-29 20:46 被阅读0次

多线程的问题

  • 正确性
    • 安全:竞争条件/死锁
//竞争条件示例:
    private static int counter = 0;

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                counter++;
            }).start();
        }
        Thread.sleep(100);
        System.out.println(counter);
    }

控制台结果:
95
- 协同:同时、随机执行的线程,如何协同工作?
      - 解决问题的办法:
      1. java原生实现
       1.1 同步: synchronized
       1.2 协同: wait() / notify() / notifyAll()
  • 效率与易用性
    • 执行地越快越好
    • 用起来越不容易出错越好

CAS(compare and swap)

  • 乐观锁:
    我现在追不到你,过一会再来问; = 自旋锁(spin lock);
private static AtomicInteger counter = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                increment();
            }).start();
        }
        Thread.sleep(100);
        System.out.println(counter);
    }

    private static void increment(){
        counter.getAndIncrement();
    }
  • 悲观锁:
    我现在追不到你,就阻塞了;
private static int counter = 0;

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                increment();
            }).start();
        }
        Thread.sleep(100);
        System.out.println(counter);
    }

    private synchronized static void increment(){ //synchronized 悲观锁
        counter++;
    }
  • 潜在问题:
    ABA问题;
    解决方案:时间戳/版本号;

  • 乐观锁 VS 悲观锁实例
    ConcurrentHashMap VS Collections.synchronizedMap / HashTable
    (Collections.synchronizedMap与hashTable基本上在所有的方法上粗暴的加了synchronized)

附:
mutex = lock
java.util.Collections.SynchronizedMap#mutex
来源于: mutual(共享) + exclusive(排他)

相关文章

  • 多线程的问题和CAS

    多线程的问题 正确性安全:竞争条件/死锁 效率与易用性执行地越快越好用起来越不容易出错越好 CAS(compare...

  • CAS和AQS

    1 CAS 什么是CAS? 如在多线程中实现自增,会出现线程安全问题,要解决这个问题,需要通过加锁的方式,调整如下...

  • CAS+ABA+Unsafe+悲观锁和乐观锁

    1 CAS CAS,即compare and swap。CAS操作是原子操作,在多线程中执行CAS操作可以实现同步...

  • Java CAS

    一、什么是 CAS? CAS是比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令。 C...

  • 二、CAS的使用与理解

    CAS(Compare And Swap) CAS(Compare And Swap),即比较并交换,是解决多线程...

  • Java CAS 底层代码实现

    CAS 什么是CAS 在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指...

  • (七) synchronized原理简单分析

    Java多线程目录 1 synchronized中各种锁是怎么竞争升级的 1 前提知识介绍 1.1 CAS CAS...

  • 高并发编程底层原理详解(1)

    CAS (compare and swap)比较并交换 在不加锁的情况,保持在多线程的一致性问题? => CA...

  • 【并发基础】- CAS原理

    CAS原理 在多线程环境下,为了提高并发的性能及数据安全性。通常采用CAS做数据的更新操作。CAS数据更新过程,先...

  • (11)CAS

    什么是CAS 简单的说,CAS就是compare and swap,翻译成中文就是比较与交换.在java多线程中,...

网友评论

      本文标题:多线程的问题和CAS

      本文链接:https://www.haomeiwen.com/subject/feplvktx.html