【JUC】13-原子类

1. CountDownLatch实现线程等待

 CountDownLatch实现线程等待。

    public static void main(String[] args) {
        AtomicInteger num = new AtomicInteger(1);
        int SIZE = 50;
        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for(int i = 0; i<SIZE; i++) {
            new Thread(()->{
                try {
                    for(int j=0; j<1000; j++) {
                        num.getAndIncrement();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(num.get());
    }

2. AtomicMarkableReference

 AtomicMarkableReference流水号为Boolean类型,只能改为true和false。

/**
 * CAS----Unsafe----do while+ABA----AtomicStampedReference,AtomicMarkableReference
 */
    static AtomicMarkableReference<Integer> atomicMarkableReference = new AtomicMarkableReference<>(100, false);
    public static void main(String[] args) {
        new Thread(()->{
            boolean marked = atomicMarkableReference.isMarked();
            Integer reference = atomicMarkableReference.getReference();
            atomicMarkableReference.compareAndSet(reference, 200, marked, !marked);
        }).start();
        new Thread(()->{
            boolean marked = atomicMarkableReference.isMarked();
            Integer reference = atomicMarkableReference.getReference();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            atomicMarkableReference.compareAndSet(reference, 300, marked, !marked);
            System.out.println("t2 " + atomicMarkableReference.isMarked() + " " + atomicMarkableReference.getReference());
        }).start();
    }

3. AtomicIntegerUpdater

 AtomicIntegerUpdater用于实现数据的原子性,但程序运行速度下降会很少。可以对对象的某一属性进行加锁,而不需要锁整个对象,但对象的这个属性需要被volatile修饰。

class BankAccount {
    // AtomicIntegerFieldUpdater 修饰的属性必须被volatile所修饰
    public volatile int money = 0;
    public synchronized void add(int cnt) {
        money = money + cnt;
    }
    AtomicIntegerFieldUpdater<BankAccount> updater = AtomicIntegerFieldUpdater.newUpdater(BankAccount.class, "money");
    public void saveMoney() {
        updater.getAndIncrement(this);
    }
}

public class NoSyncAdd {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        CountDownLatch count = new CountDownLatch(10);
        for(int i=0; i<10; i++) {
            new Thread(()->{
                for (int j = 0; j < 500; j++) {
                    account.saveMoney();
                }
                count.countDown();
            }).start();
        }
        try {
            count.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(account.money);
    }
}

4. AtomicReferenceFieldUpdater

 AtomicReferenceFieldUpdater可以用于更多的类型。

class Manager {
    public volatile Boolean isInit = false;
    AtomicReferenceFieldUpdater<Manager, Boolean> updater = AtomicReferenceFieldUpdater.newUpdater(Manager.class, Boolean.class,"isInit");
    public void init(Manager manager) {
        if (updater.compareAndSet(manager, Boolean.FALSE, Boolean.TRUE)) {
            System.out.println(Thread.currentThread().getName() + "start");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "初始化完成");
        } else {
            System.out.println(Thread.currentThread().getName() + "complete");
        }
    }
}
public class ObjectSyncInit {
    public static void main(String[] args) {
        Manager manager = new Manager();
        for(int i=0; i<10; i++) {
            new Thread(()->{
                manager.init(manager);
            }).start();
        }
    }
}

5. LongAddr和LongAccumulator实现高效的自增操作

 LongAddr和LongAccumulator实现高效的自增操作。

package com.juc.demo.Atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

class Praise {
    public int count = 0;
    public synchronized void syncAdd() {
        count ++;
    }

    public AtomicLong atomicLong = new AtomicLong(0);

    public void atomicAdd() {
        atomicLong.getAndIncrement();
    }

    public LongAdder longAdder = new LongAdder();
    public void adderAdd() {
        longAdder.increment();
    }

    public LongAccumulator accumulatorLong = new LongAccumulator((x,y)->{
        return x + y;
    },0);

    public void accumulator() {
        accumulatorLong.accumulate(1);
    }
}

public class SyncAdd {
    public static void main(String[] args) {
        Praise praise = new Praise();
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for(int i=0; i<50; i++) {
            new Thread(()->{
                for (int j = 0; j < 10000000; j++) {
                    // 36949ms
                    // praise.syncAdd();

                    // 6997ms
                    // praise.atomicAdd();

                    // 327ms
                    // praise.adderAdd();

                    // 348ms
                    praise.accumulator();
                }
                countDownLatch.countDown();
            }).start();

        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println(praise.accumulatorLong);
        System.out.println(end - start);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CRE_MO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值