zoukankan      html  css  js  c++  java
  • JUC之CAS

    /**
     * @desc CAS保证原子性
     *  T1 100 -> 101
     *  T2 100 -> 2019
     * @Author xw
     * @Date 2019/8/20
     */
    public class CasDemo {
        public static void main(String[] args) {
            AtomicInteger atomicInteger = new AtomicInteger(100);
            // T1 100 -> 101
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "	 updated: " + atomicInteger.compareAndSet(100, 101) + ",value: " + atomicInteger.get());
            }, "T1").start();
            // T2 100 -> 2019
            new Thread(() -> {
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println(Thread.currentThread().getName() + "	 updated: " + atomicInteger.compareAndSet(100, 2019) + ",value: " + atomicInteger.get());
            }, "T2").start();
        }
    }
  • 相关阅读:
    信息检索笔记
    北大课程(变态心理学)
    My life
    Excel小技巧(随机点名)
    Flask基础
    CTF
    GDB
    LD_PRELOAD
    AFL-数据变异
    AFL入门
  • 原文地址:https://www.cnblogs.com/ice-line/p/11385497.html
Copyright © 2011-2022 走看看