原子类通过(CAS和volatile)实现单共享变量的线程安全
1.单共享变量操作类
public class AtmicCounter {
private AtomicLong balance;
public AtmicCounter(long money)
{
balance=new AtomicLong(money);
System.out.println("当前金额--->"+balance);
}
//存钱
public void deposit(long money)
{
balance.addAndGet(money);
}
//取钱(取money这么多钱)
public void withDraw(long money)
{
//取得当前值
long oldValue=this.balance.get();
if(oldValue>0)
{
try {
Thread.sleep(10);
if(this.balance.compareAndSet(oldValue, oldValue-money))
{
System.out.println(Thread.currentThread().getName()+" withDraw "+oldValue +" successful!!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2.测试类
public class AtomicCounterTest extends Thread{
private AtmicCounter counter;
public AtomicCounterTest(AtmicCounter counter)
{
this.counter=counter;
}
public void run()
{
while(true){
this.counter.withDraw(1);
}
}
/**
* 银行取钱测试
*/
public static void main(String[] args) {
AtmicCounter counter=new AtmicCounter(100);
AtomicCounterTest test1=new AtomicCounterTest(counter);
AtomicCounterTest test2=new AtomicCounterTest(counter);
AtomicCounterTest test3=new AtomicCounterTest(counter);
AtomicCounterTest test4=new AtomicCounterTest(counter);
test1.start();
test2.start();
test3.start();
test4.start();
}
}
小结:对于单变量情况可以通过原子类保证线程安全,但是多个共享变量不能保证.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。