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..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/Answers.md b/Answers.md
new file mode 100644
index 0000000..dde4642
--- /dev/null
+++ b/Answers.md
@@ -0,0 +1,43 @@
+1 -
+زمانی که میخواهیم عملیات های ساده روی یک متغیر داخل thread انجام دهیم یک شیئ atomic variable داخل آن نخ میسازیم و برای انجام عملیات از توابع داخل آن کلاس استفاده میکنیم.
+این متغیر ها این خصوصیت را دارند که هنگامی که عملیاتی روی آن درحال اجراست thread دیگری اجرا نمیشود در نتیجه در محاسبه مقدار خطا ایجاد نمیشود.
+
+2 -
+AtomicInteger, AtomicLong, AtomicBoolean, AtomicReference
+
+3 - Compare locks with atomic variables.
+متغیر های اتمی برای عملیات های ساده استفاده میشود اما قفل برای عملیات های پیچیده تر استفاده میشود و برای چند متغیر استفاده میشود.
+
+🎯Bonus Task:
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class AtomicDemo {
+
+ private static int normalCounter = 0;
+ private static AtomicInteger atomicCounter = new AtomicInteger(0);
+
+ public static void main(String[] args) throws InterruptedException {
+
+ Thread[] threads = new Thread[10];
+
+ for (int i = 0; i < threads.length; i++) {
+ threads[i] = new Thread(() -> {
+ for (int j = 0; j < 10000; j++) {
+ normalCounter++;
+ atomicCounter.incrementAndGet();
+ }
+ });
+ }
+
+ for (Thread t : threads) {
+ t.start();
+ }
+
+ for (Thread t : threads) {
+ t.join();
+ }
+
+ System.out.println( normalCounter);
+ System.out.println( atomicCounter.get());
+ }
+}
\ 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..7698061 100644
--- a/src/main/java/dev/banking/model/BankAccount.java
+++ b/src/main/java/dev/banking/model/BankAccount.java
@@ -1,9 +1,12 @@
package dev.banking.model;
+import java.util.concurrent.locks.ReentrantLock;
+
public class BankAccount {
private final int accountId;
private long balance;
+ private final ReentrantLock lock = new ReentrantLock();
/*
* Students may introduce additional fields
@@ -32,7 +35,16 @@ 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 +55,14 @@ 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 +75,12 @@ 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 +97,33 @@ 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 first;
+ BankAccount second;
+
+ if (this == target) {
+ return;
+ }
+
+ if (this.getAccountId() < target.getAccountId()) {
+ first = this;
+ second = target;
+ } else {
+ first = target;
+ second = this;
+ }
+
+ first.lock.lock();
+ try {
+ second.lock.lock();
+ try {
+ this.balance -= amount;
+ target.balance += amount;
+ } finally {
+ second.lock.unlock();
+ }
+ } finally {
+ first.lock.unlock();
+ }
+
}
}
\ No newline at end of file