import java.util.concurrent.locks.
ReentrantLock;
class BankAccount {
private double balance;
private final ReentrantLock lock = new ReentrantLock();
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
lock.lock();
try {
balance += amount;
System.out.println("Deposited: $" + amount);
} finally {
lock.unlock();
}
}
public void withdraw(double amount) {
lock.lock();
try {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient balance to withdraw: $" + amount);
}
} finally {
lock.unlock();
}
}
}
public class BankingSystem {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
// Create two threads for simultaneous transactions
Thread thread1 = new Thread(() -> {
account.deposit(200.0);
account.withdraw(300.0);
});
Thread thread2 = new Thread(() -> {
account.deposit(100.0);
account.withdraw(150.0);
});
// Start the threads
thread1.start();
thread2.start();
// Wait for both threads to finish
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Print the final balance
System.out.println("Final Balance: $" + account.getBalance());
}
}
////////////////////second codes////////////////////////////
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public synchronized void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount + ", New Balance: " + balance);
}
public synchronized void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount + ", New Balance: " +
balance);
} else {
System.out.println("Insufficient balance for withdrawal.");
}
}
}
class TransactionThread extends Thread {
private BankAccount account;
private boolean isDeposit;
private double amount;
public TransactionThread(BankAccount account, boolean isDeposit, double amount)
{
this.account = account;
this.isDeposit = isDeposit;
this.amount = amount;
}
public void run() {
if (isDeposit) {
account.deposit(amount);
} else {
account.withdraw(amount);
}
}
}
public class BankingSystem {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
TransactionThread depositThread1 = new TransactionThread(account, true,
200.0);
TransactionThread withdrawThread1 = new TransactionThread(account, false,
300.0);
TransactionThread depositThread2 = new TransactionThread(account, true,
100.0);
TransactionThread withdrawThread2 = new TransactionThread(account, false,
150.0);
depositThread1.start();
withdrawThread1.start();
depositThread2.start();
withdrawThread2.start();
try {
depositThread1.join();
withdrawThread1.join();
depositThread2.join();
withdrawThread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final Balance: " + account.getBalance());
}
}