日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 原子量Atomic举例(AtomicReference)

發(fā)布時(shí)間:2025/3/15 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 原子量Atomic举例(AtomicReference) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java并發(fā)庫提供了很多原子類來支持并發(fā)訪問的數(shù)據(jù)安全性,除了常用的

AtomicInteger、AtomicBoolean、AtomicLong 外還有

AtomicReference 用以支持對(duì)象的原子操作:AtomicReference<V> 可以封裝引用一個(gè)V實(shí)例, 通過

public final boolean compareAndSet(V expect, V update)

?

可以支持并發(fā)訪問,set的時(shí)候進(jìn)行對(duì)比判斷,如果當(dāng)前值和操作之前一樣則返回false,否則表示數(shù)據(jù)沒有 變化,例如下面的代碼
使用 AtomicReference 實(shí)現(xiàn)了并發(fā)計(jì)數(shù):

package test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; public class TS { public static void main(String[] args) throws InterruptedException { dfasd111(); } private static AtomicReference<Integer> ar = new AtomicReference<Integer>(0); public static void dfasd111() throws InterruptedException { int t = 100; final int c = 100; final CountDownLatch latch = new CountDownLatch(t); for (int i = 0; i < t; i++) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < c; i++) { while (true) { Integer temp = ar.get(); if (ar.compareAndSet(temp, temp + 1)) { break; } } } latch.countDown(); } }).start(); } latch.await(); System.out.println(ar.get()); //10000000 } public final void test() { System.out.println(this.getClass()); } }

一、原子量實(shí)現(xiàn)的計(jì)數(shù)器

import java.util.HashSet; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class AtomicCounter { private AtomicInteger value = new AtomicInteger(); public int getValue() { return value.get(); } public int increase() { return value.incrementAndGet();// 內(nèi)部使用死循環(huán)for(;;)調(diào)用compareAndSet(current, next) // return value.getAndIncrement(); } public int increase(int i) { return value.addAndGet(i);// 內(nèi)部使用死循環(huán)for(;;)調(diào)用compareAndSet(current, next) // return value.getAndAdd(i); } public int decrease() { return value.decrementAndGet();// 內(nèi)部使用死循環(huán)for(;;)調(diào)用compareAndSet(current, next) // return value.getAndDecrement(); } public int decrease(int i) { return value.addAndGet(-i);// 內(nèi)部使用死循環(huán)for(;;)調(diào)用compareAndSet(current, next) // return value.addAndGet(-i); } public static void main(String[] args) { final AtomicCounter counter = new AtomicCounter(); ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { service.execute(new Runnable() { @Override public void run() { System.out.println(counter.increase()); } }); } service.shutdown(); } }

二、原子量實(shí)現(xiàn)的銀行取款

import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; public class Account { private AtomicLong balance; public Account(long money) { balance = new AtomicLong(money); System.out.println("Total Money:" + balance); } public void deposit(long money) { balance.addAndGet(money); } public void withdraw(long money) { for (; ; ) {//保證即時(shí)同一時(shí)間有人也在取款也可以再次嘗試取款,如果不需要并發(fā)嘗試取款,可以去掉這句 long oldValue = balance.get(); if (oldValue < money) { System.out.println(Thread.currentThread().getName() + " 余額不足! 余額:" + balance); break; } try {Thread.sleep(new Random().nextInt(1000));} catch (Exception e) { }// 模擬取款時(shí)間 if (balance.compareAndSet(oldValue, oldValue - money)) { System.out.println(Thread.currentThread().getName() + " 取款 " + money + " 成功! 余額:" + balance); break; } System.out.println(Thread.currentThread().getName() + " 遇到并發(fā),再次嘗試取款!"); } } public static void main(String[] args) { final Account account = new Account(1000); ExecutorService pool = Executors.newCachedThreadPool(); int i = 0; while (i++ < 13) { pool.execute(new Runnable() { @Override public void run() { account.withdraw(100); } }); } pool.shutdown(); } }

?

轉(zhuǎn)載于:https://my.oschina.net/vshcxl/blog/843787

總結(jié)

以上是生活随笔為你收集整理的java 原子量Atomic举例(AtomicReference)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。