From fc7cea97cd4824081aefb7bf084e60637aa4b882 Mon Sep 17 00:00:00 2001 From: Fatemesadat Mirabootalebi Date: Mon, 15 Jun 2026 02:37:56 -0700 Subject: [PATCH] Implement thread-safe BankAccount operations --- .idea/.gitignore | 10 ++++ .idea/compiler.xml | 13 +++++ .idea/encodings.xml | 7 +++ .idea/jarRepositories.xml | 20 +++++++ .idea/misc.xml | 12 +++++ .idea/vcs.xml | 6 +++ .../java/dev/banking/model/BankAccount.java | 51 ++++++++++++++++-- structure.txt | Bin 0 -> 2932 bytes 8 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 structure.txt diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..812c3f9 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..4158879 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eba6e1f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/dev/banking/model/BankAccount.java b/src/main/java/dev/banking/model/BankAccount.java index 745ede2..88be025 100644 --- a/src/main/java/dev/banking/model/BankAccount.java +++ b/src/main/java/dev/banking/model/BankAccount.java @@ -1,4 +1,5 @@ package dev.banking.model; +import java.util.concurrent.locks.ReentrantLock; public class BankAccount { @@ -13,7 +14,7 @@ public class BankAccount { * - Object monitor * - etc. */ - + private final ReentrantLock lock = new ReentrantLock(); public BankAccount(int accountId, long initialBalance) { this.accountId = accountId; this.balance = initialBalance; @@ -32,7 +33,13 @@ public class BankAccount { * - Should not block unnecessarily if using read/write locks */ public long getBalance() { - throw new UnsupportedOperationException("TODO: implement thread-safe balance read"); + lock.lock(); + try{ + return balance; + } + finally{ + lock.unlock(); + } } /* @@ -43,7 +50,13 @@ public class BankAccount { * - Must not lose updates under concurrency */ public void deposit(long amount) { - throw new UnsupportedOperationException("TODO: implement thread-safe deposit"); + lock.lock(); + try{ + balance += amount; + } + finally { + lock.unlock(); + } } /* @@ -56,7 +69,13 @@ public class BankAccount { * to extend the system (optional) */ public void withdraw(long amount) { - throw new UnsupportedOperationException("TODO: implement thread-safe withdraw"); + lock.lock(); + try{ + balance -= amount; + } + finally{ + lock.unlock(); + } } /* @@ -73,6 +92,28 @@ public class BankAccount { * - Or tryLock with retry strategy */ public void transfer(BankAccount target, long amount) { - throw new UnsupportedOperationException("TODO: implement atomic deadlock-free transfer"); + BankAccount lock1; + BankAccount lock2; + + if (this.accountId < target.accountId){ + lock1 = this; + lock2 = target; + } + else{ + lock1 = target; + lock2 = this; + } + + lock1.lock.lock(); + lock2.lock.lock(); + + try { + this.balance -= amount; + target.balance += amount; + } + finally{ + lock2.lock.unlock(); + lock1.lock.unlock(); + } } } \ No newline at end of file diff --git a/structure.txt b/structure.txt new file mode 100644 index 0000000000000000000000000000000000000000..7d3194001b95bc6f50e78ae246c9f432d68290f5 GIT binary patch literal 2932 zcmcgu(Qev65S-^q{Rh8SwNfcnmA(;@D3OX3B~|m%7h>F$1TextiPUdzJG*9reVC}k z1X=iO@7~Pr+}<94|C}Mi06nCbAi^bD_=M9#>QR_y1v z;p`)NUXZm*@04@KPmu~|%(+8a7Q7EC2v;~|rYGodjA`vvtd{qL86_NN&K~n=!Jp|M zbbJ1e_kQK{O59px5aHd@V)2=V<}I5LH2a=DbN=dlG)KO+QuAj$!x^_k9^s+1lJZa- zdJjk0Psll>mMv;L_R2_zX|<6NImZ9QzX`iTB-5uE%5%zv%&V2jyzYz2ke71SdpL&n zWgVV*gu%h;U2 zmunpR&sTl8IREBc19oa?oH$~El~J>x$xd%ETd+e%yaXr}N=%knw7 e-a9%}cR@yrqV8n9@TyDs@6xO~Z=1zt`|Tgz4Q4n1 literal 0 HcmV?d00001 -- 2.54.0