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();
	}
}


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值