我正在尝试做一个涉及springboot中多线程的项目。
在银行应用程序中,我可以像doWithdrawal()
那样进行synchronized
之类的操作,以保持帐户的完整性,但是我不知道如何在特定帐户级别上进行操作唯一。
由于具有不同帐号的用户应该能够同时执行提款,因此,使用我当前的代码doWithdrawal()
将使所有操作都同步,而与帐号无关。
目前,我在下面具有非常基本的功能,在这些功能中,我已使用synchronized
操作从一个帐户取款。我想要一种情形,不同的帐户持有人可以同时从自己的帐户中提款,但是,尽管同时从同一帐户中多次提款,但是该过程应该是同步的。
所以我在这里寻找的是应用程序应如何设计以具有这种功能,我知道我们可以使用@Async
进行异步操作,但不确定在撤回相同操作的情况下如何使用帐户应该是同步的,但不同的应该是异步的。
Account.java
public class Account {
int number ;
float currentBalance ;
float openingBalance ;
String accountHolder ;
String branch ;
float rateOfInterest ;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public float getCurrentBalance() {
return currentBalance;
}
public void setCurrentBalance(float currentBalance) {
this.currentBalance = currentBalance;
}
public float getOpeningBalance() {
return openingBalance;
}
public void setOpeningBalance(float openingBalance) {
this.openingBalance = openingBalance;
}
public String getAccountHolder() {
return accountHolder;
}
public void setAccountHolder(String accountHolder) {
this.accountHolder = accountHolder;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public float getRateOfInterest() {
return rateOfInterest;
}
public void setRateOfInterest(float rateOfInterest) {
this.rateOfInterest = rateOfInterest;
}
}
App.java
public class App {
public static void main(String[] args) {
BankService.openAccount(1001);
Thread2 t2 = new Thread2();
Thread3 t3 = new Thread3();
Thread4 t4 = new Thread4();
t2.start();
t3.start();
t4.start();
}
}
class Thread2 extends Thread {
BankService bs = BankService.getInstance();
public void run(){
System.out.println("bs instrance is"+bs.hashCode());
bs.doWithdrawal(200, 1001);
}
}
class Thread3 extends Thread {
BankService bs = BankService.getInstance();
public void run(){
System.out.println("bs instrance is"+bs.hashCode());
bs.doWithdrawal(100, 1001);
}
}
class Thread4 extends Thread {
BankService bs = BankService.getInstance();
public void run(){
System.out.println("bs instrance is"+bs.hashCode());
bs.doWithdrawal(100, 1001);
}
}
BankService.java
import java.util.HashMap;
import java.util.Map;
public class BankService {
private static BankService bankService ;
private BankService(){
}
public static BankService getInstance(){
if(bankService==null){
return bankService = new BankService();
}
return bankService;
}
static Map<Integer,Account> accountMap = new HashMap<Integer, Account>();
public void doDeposit(float amount,int accountNum){
Account acc = accountMap.get(accountNum);
acc.setCurrentBalance(acc.getCurrentBalance()+amount);
System.out.println("made deposit");
System.out.println("account : "+acc.getNumber()+" "+acc.getCurrentBalance());
}
public synchronized void doWithdrawal(float amount,int accountNum){
Account acc = accountMap.get(accountNum);
acc.setCurrentBalance(acc.getCurrentBalance()-amount);
System.out.println("account : "+acc.getNumber()+" "+acc.getCurrentBalance());
}
public void checkBalance(int accountNumber){
}
public static void openAccount(int accnum){
Account acc = new Account();
acc.setCurrentBalance(2000);
acc.setNumber(1001);
accountMap.put(acc.getNumber(), acc);
System.out.println("obj is"+accountMap);
}
}
答案 0 :(得分:1)
您无需同步整个方法,而可以按照以下方式在特定的“ accountNum”上进行同步:
public void doWithdrawal(int accNum) {
synchronized(accountMap.get(accNum)) {
// now perform withdrawal
}
}
这样,您将仅在帐号相同时进行同步。