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..a9076af
--- /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..652d615
--- /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..77b80eb 100644
--- a/src/main/java/dev/banking/model/BankAccount.java
+++ b/src/main/java/dev/banking/model/BankAccount.java
@@ -1,9 +1,13 @@
package dev.banking.model;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
public class BankAccount {
private final int accountId;
private long balance;
+ private Lock lock = new ReentrantLock();
/*
* Students may introduce additional fields
@@ -32,7 +36,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");
+// throw new UnsupportedOperationException("TODO: implement thread-safe balance read");
+ try {
+ lock.lock();
+ return this.balance;
+ } finally {
+ lock.unlock();
+ }
}
/*
@@ -43,7 +53,13 @@ public class BankAccount {
* - Must not lose updates under concurrency
*/
public void deposit(long amount) {
- throw new UnsupportedOperationException("TODO: implement thread-safe deposit");
+// throw new UnsupportedOperationException("TODO: implement thread-safe deposit");
+ try {
+ lock.lock();
+ this.balance += amount;
+ } finally {
+ lock.unlock();
+ }
}
/*
@@ -56,7 +72,13 @@ public class BankAccount {
* to extend the system (optional)
*/
public void withdraw(long amount) {
- throw new UnsupportedOperationException("TODO: implement thread-safe withdraw");
+// throw new UnsupportedOperationException("TODO: implement thread-safe withdraw");
+ try {
+ lock.lock();
+ this.balance -= amount;
+ } finally {
+ lock.unlock();
+ }
}
/*
@@ -73,6 +95,29 @@ public class BankAccount {
* - Or tryLock with retry strategy
*/
public void transfer(BankAccount target, long amount) {
- throw new UnsupportedOperationException("TODO: implement atomic deadlock-free transfer");
+
+ if (this.accountId < target.accountId) {
+ this.lock.lock();
+ target.lock.lock();
+ } else if (this == target) {
+ return;
+ } else {
+ target.lock.lock();
+ this.lock.lock();
+ }
+
+ try {
+ this.withdraw(amount);
+ target.deposit(amount);
+ } finally {
+ if (this.accountId < target.accountId) {
+ target.lock.unlock();
+ this.lock.unlock();
+ } else {
+ this.lock.unlock();
+ target.lock.unlock();
+ }
+ }
+
}
}
\ No newline at end of file