feat(bank-account): add student implementation template for concurrency assignment
- Provide clean BankAccount skeleton for student implementation - Define thread-safety responsibilities for getBalance, deposit, withdraw, and transfer - Add detailed TODO comments clarifying concurrency and deadlock requirements - Include hints for lock strategy and global ordering for transfer method - Ensure compatibility with DemoApplication and stress testing infrastructure
This commit is contained in:
@@ -7,7 +7,11 @@ public class BankAccount {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Students may introduce additional fields
|
* Students may introduce additional fields
|
||||||
* if required by their synchronization strategy.
|
* such as:
|
||||||
|
* - Lock / ReentrantLock
|
||||||
|
* - ReadWriteLock
|
||||||
|
* - Object monitor
|
||||||
|
* - etc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public BankAccount(int accountId, long initialBalance) {
|
public BankAccount(int accountId, long initialBalance) {
|
||||||
@@ -20,34 +24,55 @@ public class BankAccount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO
|
* TODO:
|
||||||
* Implement a thread-safe balance reader.
|
* Return the current balance in a thread-safe way.
|
||||||
|
*
|
||||||
|
* Requirements:
|
||||||
|
* - Must be safe under concurrent reads/writes
|
||||||
|
* - Should not block unnecessarily if using read/write locks
|
||||||
*/
|
*/
|
||||||
public long getBalance() {
|
public long getBalance() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException("TODO: implement thread-safe balance read");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO
|
* TODO:
|
||||||
* Implement a thread-safe deposit operation.
|
* Increase balance atomically.
|
||||||
|
*
|
||||||
|
* Requirements:
|
||||||
|
* - Must not lose updates under concurrency
|
||||||
*/
|
*/
|
||||||
public void deposit(long amount) {
|
public void deposit(long amount) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException("TODO: implement thread-safe deposit");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO
|
* TODO:
|
||||||
* Implement a thread-safe withdrawal operation.
|
* Decrease balance atomically.
|
||||||
|
*
|
||||||
|
* Requirements:
|
||||||
|
* - Must not cause race conditions
|
||||||
|
* - Negative balance handling is NOT required unless you decide
|
||||||
|
* to extend the system (optional)
|
||||||
*/
|
*/
|
||||||
public void withdraw(long amount) {
|
public void withdraw(long amount) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException("TODO: implement thread-safe withdraw");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO
|
* TODO:
|
||||||
* Implement an atomic and deadlock-free transfer.
|
* Transfer money between two accounts atomically.
|
||||||
|
*
|
||||||
|
* IMPORTANT REQUIREMENTS:
|
||||||
|
* - Must be atomic (no partial transfer)
|
||||||
|
* - Must be deadlock-free
|
||||||
|
* - Must protect both source and target accounts
|
||||||
|
*
|
||||||
|
* HINT:
|
||||||
|
* - Consider global lock ordering using accountId
|
||||||
|
* - Or tryLock with retry strategy
|
||||||
*/
|
*/
|
||||||
public void transfer(BankAccount target, long amount) {
|
public void transfer(BankAccount target, long amount) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException("TODO: implement atomic deadlock-free transfer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user