java 原子量Atomic举例(AtomicReference)
java并发库提供了很多原子类来支持并发访问的数据安全性,除了常用的
AtomicInteger、AtomicBoolean、AtomicLong 外还有
AtomicReference 用以支持对象的原子操作:AtomicReference<V> 可以封装引用一个V实例, 通过
public final boolean compareAndSet(V expect, V update)
可以支持并发访问,set的时候进行对比判断,如果当前值和操作之前一样则返回false,否则表示数据没有 变化,例如下面的代码
使用 AtomicReference 实现了并发计数:
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()); } }
一、原子量实现的计数器
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();// 内部使用死循环for(;;)调用compareAndSet(current, next) // return value.getAndIncrement(); } public int increase(int i) { return value.addAndGet(i);// 内部使用死循环for(;;)调用compareAndSet(current, next) // return value.getAndAdd(i); } public int decrease() { return value.decrementAndGet();// 内部使用死循环for(;;)调用compareAndSet(current, next) // return value.getAndDecrement(); } public int decrease(int i) { return value.addAndGet(-i);// 内部使用死循环for(;;)调用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(); } }
二、原子量实现的银行取款
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 (; ; ) {//保证即时同一时间有人也在取款也可以再次尝试取款,如果不需要并发尝试取款,可以去掉这句 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) { }// 模拟取款时间 if (balance.compareAndSet(oldValue, oldValue - money)) { System.out.println(Thread.currentThread().getName() + " 取款 " + money + " 成功! 余额:" + balance); break; } System.out.println(Thread.currentThread().getName() + " 遇到并发,再次尝试取款!"); } } 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(); } }