有没有可能在没有锁的情况下快速解决多线程银行账户问题?

huangapple go评论83阅读模式
英文:

Is it possible to solve the multithreaded bank account problem fast without locks?

问题

I'm trying to solve the multithreaded bank account problem* without using locks but using multiversion concurrency control. It's working. It's just a bit slow. How can I speed it up?

(*) I have 5 users, each starting with 200 - each randomly withdrawing 100 and depositing 100 into another bank account owned by another user. I expect bank balances to total 1000 by the end of run. No money should be lost or created. This part works with my implementation below.

import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Function;

public class ConcurrentWithdrawer {

    private Map<String, Integer> database = new HashMap<>();
    private int transactionCount = 0;
    private final List<Transaction> transactions = Collections.synchronizedList(new ArrayList<>());

    public static void main(String[] args) {
        try {
            new ConcurrentWithdrawer().run();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static int getRandomNumberInRange(int min, int max) {
        // ... (same as original code)
    }

    public void run() throws ExecutionException, InterruptedException {
        // ... (same as original code)

        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);

        List<Future> futures = new ArrayList<Future>();
        for (int i = 0; i < 5; i++) {
            futures.add(executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    // ... (same as original code)
                }
            }));
        }

        // ... (same as original code)

        executor.shutdown();
    }

    // ... (same as original code)

    private class Transaction {
        // ... (same as original code)
    }

    private interface TransactionStep {
        TransactionContext run(TransactionContext context);
    }

    private class ReadStep implements TransactionStep {
        // ... (same as original code)
    }

    private class TransactionContext {
        // ... (same as original code)
    }

    private class WriteStep implements TransactionStep {
        // ... (same as original code)
    }

    private class WriteContext {
        // ... (same as original code)
    }
}

Output I receive:

Retry count was 4511
Retry count was 671
Retry count was 5956
Retry count was 140
Retry count was 3818
Retry count was 3102
Retry count was 34
Retry count was 580
Retry count was 106
Retry count was 46
Retry count was 22
Retry count was 11478
Retry count was 199
Retry count was 33
Retry count was 715
Retry count was 263
Retry count was 6186
Retry count was 6846
Retry count was 7012
Retry count was 301
Retry count was 93
Retry count was 148
Retry count was 11
Retry count was 355
Retry count was 7
Totals while running
1000
1000
1000
1000
1000
Expected money
1000
Final money
account0 200
account1 700
account2 100
account3 0
account4 0
1000
BUILD SUCCESSFUL in 515ms
英文:

I'm trying to solve the multithreaded bank account problem* without using locks but using multiversion concurrency control. It's working. It's just a bit slow. How can I speed it up?

(*) I have 5 users, each starting with 200 - each randomly withdrawing 100 and depositing 100 into another bank account owned by another user. I expect bank balances to total 1000 by the end of run. No money should be lost or created. This part works with my implementation below.

import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Function;
public class ConcurrentWithdrawer {
private Map&lt;String, Integer&gt; database = new HashMap&lt;&gt;();
private int transactionCount = 0;
private final List&lt;Transaction&gt; transactions = Collections.synchronizedList(new ArrayList&lt;&gt;());
public static void main(String[] args) {
try {
new ConcurrentWithdrawer().run();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static int getRandomNumberInRange(int min, int max) {
if (min &gt;= max) {
throw new IllegalArgumentException(&quot;max must be greater than min&quot;);
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
public void run() throws ExecutionException, InterruptedException {
int startAmount = 200;
int numberAccounts = 5;
int totalMoney = 0;
for (int i = 0; i &lt; numberAccounts; i++) {
database.put(String.format(&quot;account%d&quot;, i), startAmount);
totalMoney += startAmount;
}
ThreadPoolExecutor executor =
(ThreadPoolExecutor) Executors.newFixedThreadPool(5);
List&lt;Future&gt; futures = new ArrayList&lt;Future&gt;();
for (int i = 0; i &lt; 5; i++) {
futures.add(executor.submit(new Callable&lt;Integer&gt;() {
@Override
public Integer call() {
for (int j = 0; j &lt; 5; j++) {
Transaction transaction = beginTransaction(transactions, database);
transaction.read(&quot;fromBalance&quot;, &quot;fromAccountName&quot;, (context) -&gt; {
int fromAccount = getRandomNumberInRange(0, 4);
String fromAccountName = String.format(&quot;account%d&quot;, fromAccount);
return fromAccountName;
}).read(&quot;toBalance&quot;, &quot;toAccountName&quot;, (context) -&gt; {
int toAccount = getRandomNumberInRange(0, 4);
String toAccountName = String.format(&quot;account%d&quot;, toAccount);
while (toAccountName.equals(context.lookupName(&quot;fromAccountName&quot;))) {
toAccount = getRandomNumberInRange(0, 4);
toAccountName = String.format(&quot;account%d&quot;, toAccount);
}
return toAccountName;
}).write(&quot;fromAccountName&quot;, (writeContext) -&gt; {
int difference;
TransactionContext context = writeContext.context;
if (context.get(&quot;fromBalance&quot;) &gt;= 100) {
difference = 100;
} else {
difference = 0;
}
context.write(writeContext.writeStep, &quot;fromAccountName&quot;, context.get(&quot;fromBalance&quot;) - difference);
context.put(&quot;difference&quot;, difference);
}).write(&quot;toAccountName&quot;, (writeContext) -&gt; {
TransactionContext context = writeContext.context;
context.write(writeContext.writeStep, &quot;toAccountName&quot;, context.get(&quot;toBalance&quot;) + context.get(&quot;difference&quot;));
}).commit();
}
int foundMoney = 0;
for (int j = 0; j &lt; numberAccounts; j++) {
Integer foundMoney1;
String account = String.format(&quot;account%d&quot;, j);
foundMoney1 = database.get(account);
foundMoney += foundMoney1;
}
return foundMoney;
}
}));
}
List&lt;Integer&gt; monies = new ArrayList&lt;&gt;();
for (Future f : futures) {
int foundMoney = (Integer) f.get();
monies.add(foundMoney);
}
System.out.println(&quot;Totals while running&quot;);
for (Integer money : monies) {
System.out.println(money);
}
System.out.println(&quot;Expected money&quot;);
System.out.println(totalMoney);
System.out.println(&quot;Final money&quot;);
int foundMoney = 0;
for (int j = 0; j &lt; numberAccounts; j++) {
Integer foundMoney1;
foundMoney1 = database.get(String.format(&quot;account%d&quot;, j));
System.out.println(String.format(String.format(&quot;account%d %d&quot;, j, foundMoney1)));
foundMoney += foundMoney1;
}
System.out.println(foundMoney);
executor.shutdown();
}
private Transaction beginTransaction(List&lt;Transaction&gt; transactions, Map&lt;String, Integer&gt; database) {
transactionCount = transactionCount + 1;
Transaction transaction = new Transaction(transactions, transactionCount, database);
this.transactions.add(transaction);
return transaction;
}
private class Transaction {
public Long readTimestamp = 0L;
public Long writeTimestamp = 0L;
public List&lt;String&gt; readTargets = new ArrayList&lt;&gt;();
private List&lt;Transaction&gt; transactions;
private final int id;
private Map&lt;String, Integer&gt; database;
private List&lt;TransactionStep&gt; steps = new ArrayList&lt;&gt;();
private TransactionContext transactionContext = new TransactionContext();
private boolean active = true;
private boolean cancel = false;
private long transactionFinish;
private long transactionStart;
private int reread;
private boolean valid;
public Transaction(List&lt;Transaction&gt; transactions, int id, Map&lt;String, Integer&gt; database) {
this.transactions = transactions;
this.id = id;
this.database = database;
}
public Transaction read(String field, String name, Function&lt;TransactionContext, String&gt; keyGetter) {
ReadStep step = new ReadStep(this, field, keyGetter);
steps.add(step);
transactionContext.registerStep(name, step);
return this;
}
public Transaction write(String fieldName, Consumer&lt;WriteContext&gt; writer) {
steps.add(new WriteStep(this, fieldName, writer));
return this;
}
public boolean invalid() {
long largestWrite = 0L;
long largestRead = 0L;
List&lt;Transaction&gt; cloned = new ArrayList&lt;&gt;(transactions);
cloned.sort(new Comparator&lt;Transaction&gt;() {
@Override
public int compare(Transaction o1, Transaction o2) {
return (int) (o1.transactionStart - o2.transactionStart);
}
});
for (Transaction transaction : cloned) {
ArrayList&lt;TransactionStep&gt; clonedSteps = new ArrayList&lt;&gt;(transaction.steps);
for (TransactionStep step : clonedSteps) {
for (TransactionStep thisStep : steps) {
if (step instanceof ReadStep &amp;&amp; thisStep instanceof ReadStep) {
ReadStep thisReadStep = (ReadStep) thisStep;
ReadStep readStep = (ReadStep) step;
if (thisReadStep.key.equals(readStep.key)) {
if (thisReadStep.timestamp &gt; readStep.timestamp) {
return true;
}
}
}
if (step instanceof WriteStep &amp;&amp; thisStep instanceof WriteStep) {
WriteStep thisWriteStep = (WriteStep) thisStep;
WriteStep writeStep = (WriteStep) step;
if (thisWriteStep.timestamp &gt; writeStep.timestamp) {
return true;
}
}
}
}
}
return false;
}
public void commit() {
boolean needsRunning = true;
int retryCount = 0;
transactionStart = System.nanoTime();
while (needsRunning || invalid()) {
readTimestamp = 0L;
writeTimestamp = 0L;
readTargets.clear();
retryCount++;
active = true;
for (TransactionStep step : steps) {
step.run(transactionContext);
}
needsRunning = false;
if (cancel) {
needsRunning = true;
cancel = false;
}
}
System.out.println(String.format(&quot;Retry count was %d&quot;, retryCount));
for (TransactionStep step : steps) {
if (step instanceof ReadStep) {
String key = ((ReadStep) step).key;
Integer value = transactionContext.context.get(key);
database.put(key, value);
}
}
transactions.remove(this);
transactionFinish = System.nanoTime();
}
}
private interface TransactionStep {
TransactionContext run(TransactionContext context);
}
private class ReadStep implements TransactionStep {
private final String field;
private final Function&lt;TransactionContext, String&gt; keyGetter;
private boolean activated;
private String key;
public long timestamp;
Transaction transaction;
public ReadStep(Transaction transaction, String field, Function keyGetter) {
this.transaction = transaction;
this.field = field;
this.keyGetter = keyGetter;
this.activated = false;
}
public TransactionContext run(TransactionContext context) {
if (!activated) {
key = (String) this.keyGetter.apply(context);
}
activated = true;
timestamp = System.nanoTime();
context.put(field, database.get(key));
if (transaction.readTimestamp == 0L) {
transaction.readTimestamp = timestamp;
}
transaction.readTargets.add(key);
return context;
}
}
private class TransactionContext {
public final HashMap&lt;String, Integer&gt; context;
private Map&lt;String, ReadStep&gt; readSteps = new HashMap&lt;&gt;();
public TransactionContext() {
this.context = new HashMap&lt;&gt;();
}
public void registerStep(String name, ReadStep readStep) {
readSteps.put(name, readStep);
}
public void put(String field, Integer integer) {
this.context.put(field, integer);
}
public String lookupName(String name) {
return readSteps.get(name).key;
}
public void write(WriteStep writeStep, String name, Integer newValue) {
String key = lookupName(name);
writeStep.key = key;
context.put(key, newValue);
}
public Integer get(String field) {
return this.context.get(field);
}
}
private class WriteStep implements TransactionStep {
public String key;
private boolean activated;
private String fieldName;
private final Consumer&lt;WriteContext&gt; writer;
public long timestamp;
Transaction transaction;
public WriteStep(Transaction transaction, String fieldName, Consumer&lt;WriteContext&gt; writer) {
this.transaction = transaction;
this.fieldName = fieldName;
this.writer = writer;
activated = false;
}
@Override
public TransactionContext run(TransactionContext context) {
timestamp = System.nanoTime();
transaction.writeTimestamp = timestamp;
writer.accept(new WriteContext(this, context));
activated = true;
return context;
}
}
private class WriteContext {
private final WriteStep writeStep;
private final TransactionContext context;
public WriteContext(WriteStep writeStep, TransactionContext context) {
this.writeStep = writeStep;
this.context = context;
}
}
}

Output I receive:

Retry count was 4511
Retry count was 671
Retry count was 5956
Retry count was 140
Retry count was 3818
Retry count was 3102
Retry count was 34
Retry count was 580
Retry count was 106
Retry count was 46
Retry count was 22
Retry count was 11478
Retry count was 199
Retry count was 33
Retry count was 715
Retry count was 263
Retry count was 6186
Retry count was 6846
Retry count was 7012
Retry count was 301
Retry count was 93
Retry count was 148
Retry count was 11
Retry count was 355
Retry count was 7
Totals while running
1000
1000
1000
1000
1000
Expected money
1000
Final money
account0 200
account1 700
account2 100
account3 0
account4 0
1000
BUILD SUCCESSFUL in 515ms

How do I make it efficient? I'm sure Postgres doesn't let transactions run thousands of times before admitting them.

Someone said the code was obfuscated. This code reads a value (and records a read) like an SQL read statement. The code needs access to the name of the field being accessed as well as the actual value being accessed. This is why the code is written like this. The following code says: Read the field name generated by this function, store the name into fromAccountName and store the resulting value into fromBalance.

transaction.read(&quot;fromBalance&quot;, &quot;fromAccountName&quot;, (context) -&gt; {
int fromAccount = getRandomNumberInRange(0, 4);
String fromAccountName = String.format(&quot;account%d&quot;, fromAccount);
return fromAccountName;
})

答案1

得分: 3

系统测试的简要描述,从问题中推断并查看代码得出:

  • 账户信息存储在一个“数据库”中,该数据库由账户名称映射到账户余额组成。
  • 账户之间的交易应始终具有原子性,以确保不会创建或销毁超出初始金额200的资金。
  • 另一个假设是账户余额不会为负值。
  • 在您的示例代码中,数据库的实现为Map<String, Integer>

解决原子性转账问题的方法是使用一个名为BankAccounts的账户数据库,如下所示。下面的代码排除了测试代码,但解决了一致性问题。

public class BankAccounts {
    /**
     * 每次转账的重试次数
     */
    public static final int RETRIES = 10;
    /**
     * 账户数据库
     */
    private Map<String, AtomicInteger> accounts = new HashMap<>();

    /**
     * 创建每个账户的初始余额为200。
     */
    public BankAccounts() {
        // 初始账户余额的填充
        // 留给读者作为练习
    }

    /**
     * 返回包含所有账户名称的集合。
     */
    public Set<String> accountNames() {
        return Collections.unmodifiableSet(accounts.keySet());
    }

    /**
     * 获取指定账户的余额。
     */
    public int getBalanceFor(String accountName) {
        AtomicInteger account = accounts.get(accountName);
        return account != null ? account.get() : 0;
    }

    /**
     * 原子性地从一个账户转账到另一个账户,如果成功则返回{@code true}。
     */
    public boolean transfer(String fromAccountName, String toAccountName, int amount) {
        AtomicInteger fromBalance = accounts.get(fromAccountName);
        AtomicInteger toBalance = accounts.get(toAccountName);
        if (amount < 0 || fromBalance == null || toBalance == null) {
            return false;
        }

        for (int retry = 0; retry < RETRIES; retry++) {
            int fromValue = fromBalance.get();  // 获取转出账户余额值
            if (fromValue >= amount) { // 检查是否有足够的钱
                if (fromBalance.compareAndSet(fromValue, fromValue - amount)) {
                    toBalance.addAndGet(amount); // 始终允许加钱 ;-)
                    return true;
                } else {
                    // fromBalance的值在并发情况下被修改
                    Thread.yield(); // 可选的,用于让出一些执行时间给其他线程
                }
            }
        }
        return false;
    }
}

注意:上述是您提供的代码的翻译,我已经按照您的要求只返回了翻译好的部分。

英文:

Short description of the system being tested, deducted from the question and looking at the code:

  • Accounts are kept in a "database" that has that consists of account names mapped to account values.
  • Transactions between accounts should always be atomic in nature, so that no money but the initial money of 200 is created or destroyed.
  • An additional assumption is that accounts cannot have a negative value.
  • In your example code, the database is implemented as a Map&lt;String,Integer&gt;.

The problem transfers being atomic can be solved by using an account database BankAccounts as follows. This excludes the test code, but solves the problem of consistency.

public class BankAccounts {
/**
* The number of retries for each transfer
*/
public static final int RETRIES = 10;
/**
* The account database
*/
private Map&lt;String, AtomicInteger&gt; accounts = new HashMap&lt;&gt;();
/**
* Creates accounts with each 200 initial balance.
*/
public BankAccounts() {
// fill the accounts initially
// Left as an exercice to the reader
}
/**
* Return a set with all account names.
*/
public Set&lt;String&gt; accountNames() {
return Collections.unmodifiableSet(accounts.keySet());
}
/**
* Get the balance value for the specified account.
*/
public int getBalanceFor(String accountName) {
AtomicInteger account = accounts.get(accountName);
return account != null ? account.get() : 0;
}
/**
* Atomically transfers an amount from one account to another and returns {@code true} if that was successful.
*/
public boolean transfer(String fromAccountName, String toAccountName, int amount) {
AtomicInteger fromBalance = accounts.get(fromAccountName);
AtomicInteger toBalance = accounts.get(toAccountName);
if (amount &lt; 0 || fromBalance == null || toBalance == null) {
return false;
}
for (int retry = 0; retry &lt; RETRIES; retry++) {
int fromValue = fromBalance.get();  // get from-account balance value
if (fromValue &gt;= amount) { // check if enough money
if (fromBalance.compareAndSet(fromValue, fromValue - amount)) {
toBalance.addAndGet(amount); // Adding money is always allowed ;-) 
return true;
} else {
// value of fromBalance was changed concurrently
Thread.yield(); // optional.
}
}
}
return false;
}
}

huangapple
  • 本文由 发表于 2020年10月10日 17:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64291915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定