A robust, thread-safe banking application that demonstrates concurrent programming principles in Java. The application simulates a banking system where multiple threads simultaneously handle deposits and withdrawals while maintaining transaction consistency and preventing race conditions.
- Thread-Safe Operations: Secure handling of concurrent deposits and withdrawals
- Lock-Based Synchronization: Uses
ReentrantLockfor thread coordination - Condition Variables: Implements
Conditionfor thread communication - Random Transaction Amounts: Simulates real-world random deposit and withdrawal scenarios
- Deadlock Prevention: Fair locking mechanism to prevent thread starvation
- Real-time Balance Updates: Immediate balance reflection after each transaction
ReentrantLockwith fairness policy enabledConditionvariables for thread synchronizationExecutorServicefor thread pool management- Thread sleep simulation for deposit operations
- Automated thread shutdown handling
- Thread-safe balance management
- Conditional withdrawal processing
- Synchronized deposit handling
- Real-time balance tracking
- Deposit Thread: Generates random deposits (1-10 units) every second
- Withdrawal Thread: Attempts random withdrawals (1-10 units) continuously
- Java Development Kit (JDK) 8 or higher
- Java IDE (optional) - Eclipse, IntelliJ IDEA, or VS Code
- Clone the repository
git clone https://github.com/yourusername/java-multithreaded-banking.git
cd java-multithreaded-banking- Compile the application
javac AccountWithConditions.java- Run the application
java AccountWithConditionsThread 1 Thread 2 Balance
Deposit 5 5
Wait for a deposit
Deposit 3 8
Withdraw 6 2
Deposit 4 6
Withdraw 5 1
private static Lock lock = new ReentrantLock(true);
private static Condition newDeposit = lock.newCondition();- Fair lock ensures FIFO thread access
- Condition variable manages deposit-withdrawal coordination
public void deposit(int amount) {
lock.lock();
try {
balance += amount;
newDeposit.signalAll();
}
finally {
lock.unlock();
}
}public void withdraw(int amount) {
lock.lock();
try {
while(balance < amount) {
newDeposit.await();
}
balance -= amount;
}
finally {
lock.unlock();
}
}-
Atomic Operations
- All balance modifications occur within locked sections
- Complete transaction isolation
-
State Consistency
- Balance never goes negative
- All withdrawals are fully backed by deposits
-
Resource Management
- Proper lock release through
finallyblocks - Managed thread pool for controlled concurrency
- Proper lock release through
java-multithreaded-banking/
├── AccountWithConditions.java # Main application file
├── README.md # Documentation
├── LICENSE.txt # License information
└── multithreaded_banking_app_demo.jpg # Application demo
- Fork the repository
- Create a feature branch (
git checkout -b feature/enhancement) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/enhancement) - Create a Pull Request
This project is licensed under the MIT License - see the LICENSE.txt file for details.
For questions and feedback, please open an issue in the repository.
