使用atomic實現銀行存取款

[size=xx-large]一、應用場景[/size]

[size=medium] 在通常情況下,我們在用多線程處理一個問題時,當多個線程要同時處理一個共享的數據時,往往需要通過synchronized的進行同步,在線程比較多的情況會導致鎖競爭太厲害,大部分的時間都花費在了線程切換之間,而對實際的業務缺操作的相對比較少。

下面我們用java.util.concurrent.atomic來實現一個類似的功能,以銀行存取款爲例子:[/size]


public class AtomicAccount {


AtomicLong balance;

public AtomicAccount(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,int delay) {
long oldvalue = balance.get();
if(oldvalue>=money) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[color=red] //如果balance和oldvalue相同,則balance=oldvalue - money;
if(balance.compareAndSet(oldvalue, oldvalue - money)) {[/color]
System.out.println(Thread.currentThread().getName()+" withdraw " + money +" successfull!" + "balance="+balance);
} else {
System.out.println(Thread.currentThread().getName()+" withdraw " + money +" failed!" + "balance="+balance);
}
} else {
System.out.println(Thread.currentThread().getName()+" withdraw " + money +" balance is not enough,withdraw failed!" + "balance="+balance);
}
}
}




public class AtomicAccountTest extends Thread{

AtomicAccount account;

int delay;

public AtomicAccountTest(AtomicAccount account , int delay) {
this.account = account;
this.delay = delay;
}

public void run() {
account.withdraw(100, delay);
}

public static void main(String[] args) {

AtomicAccount account = new AtomicAccount(100);
AtomicAccountTest thread_1 = new AtomicAccountTest(account,1000);
AtomicAccountTest thread_2 = new AtomicAccountTest(account,0);

thread_1.start();
thread_2.start();
}
}




運行結果:

Total Money:100
Thread-1 withdraw 100 successfull!balance=0
Thread-0 withdraw 100 failed!balance=0


[size=xx-large]二、說明:[/size]

[size=medium]//如果balance和oldvalue相同,則balance=oldvalue - money;
執行到if(balance.compareAndSet(oldvalue, oldvalue - money)) 的時候,會判斷balance與oldvalue是否相同,如果相同,說明沒有程序篡改,則balance=oldvalue - money[/size]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章