当前位置: 首页 > news >正文

【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();// 348mspraise.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);}
}

http://www.mrgr.cn/news/23032.html

相关文章:

  • C++学习笔记----6、内存管理(五)---- 智能指针(1)
  • 学习threejs,创建立方体,并执行旋转动画
  • 本地服务器部署Text generation并添加code llama实现远程多人协作
  • 线程池的应用
  • 九月五日(k8s配置)
  • 外贸人提高潜在客户EDM电子邮件营销参与度的一些建议
  • 电池的电-热-寿命模型是什么?
  • 在debian11下的tightvncserver配置
  • 安全产品概述
  • oracle数据库报ORA-00060错误(死锁)的解决办法
  • AI人工智能如何重塑我们的世界,引领无限可能
  • 收藏:B站相当精彩的关于向量数据库的2个视频
  • 百望云 X 爱普生 :对接乐企实现税企直连,节省数十万元管理成本!
  • std::fixed
  • windows下载安装perl
  • java中普通代码块和静态代码块之间的区别(java小知识点)
  • 线程之间的通信方法
  • 智算时空 重塑视界│智汇云舟2024视频孪生产品发布会圆满举行,多个“全球首款”重磅亮相
  • 《花100块做个摸鱼小网站! 》第六篇—将小网站部署到云服务器上
  • opencv学习:图像轮廓识别及代码实现