6 ms·
You'd need to lock both accounts... but even then. Doing any kind of accounting operations without being atomic, transactional or idempotent is just wrong. Even
by asoo 7y ago
You'd need to lock both accounts... but even then. Doing any kind of accounting operations without being atomic, transactional or idempotent is just wrong. Even with single call - acct2.add() could fail and you're left with broken state.
If this is in-memory state then you don't need asyncs at all. If it's not, Locks just give you false sense of safety.