1 Commits
Author SHA1 Message Date
2025mohseni 052a48dddb exercises : Solve multithreading 2026-06-04 08:42:42 +03:30
4 changed files with 51 additions and 26 deletions
@@ -20,18 +20,19 @@ public class DeadlockPreventionWorkshop {
}
public static void useResources(Resource r1, Resource r2) {
// TODO: Prevent deadlock by enforcing a consistent lock acquisition order
// Hint: Compare resource IDs and always synchronize on the resource with the smaller ID first.
// تشخیص اینکه کدام منبع ID کوچکتر و کدام بزرگتر دارد
Resource firstLock = r1.getId() < r2.getId() ? r1 : r2;
Resource secondLock = r1.getId() < r2.getId() ? r2 : r1;
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName());
synchronized (r1) {
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.getName());
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + firstLock.getName());
synchronized (firstLock) {
System.out.println(" " + Thread.currentThread().getName() + " locked " + firstLock.getName());
try { Thread.sleep(100); } catch (InterruptedException ignored) {}
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r2.getName());
synchronized (r2) {
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r2.getName());
System.out.println(Thread.currentThread().getName() + " using " + r1.getName() + " and " + r2.getName());
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + secondLock.getName());
synchronized (secondLock) {
System.out.println(" " + Thread.currentThread().getName() + " locked " + secondLock.getName());
System.out.println(Thread.currentThread().getName() + " is using " + firstLock.getName() + " and " + secondLock.getName());
}
}
}
@@ -6,15 +6,19 @@ import java.util.concurrent.locks.ReentrantLock;
public class LockWorkshop {
public static int counter = 0;
// TODO: Create a ReentrantLock instance to protect the critical section
private static final java.util.concurrent.locks.ReentrantLock lock = new java.util.concurrent.locks.ReentrantLock();
public static class MyRunnable implements Runnable {
@Override
public void run() {
int i;
for (i = 0; i < 1_000_000; i++) {
// TODO: Acquire the lock, increment the counter, and release the lock safely in a finally block
lock.lock();
try {
counter += 1;
} finally {
lock.unlock();
}
}
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
}
@@ -3,16 +3,17 @@ package workshop.exercises;
public class SynchronizedWorkshop {
public static int counter = 0;
// TODO: Define a lock object to prevent race condition
private static final Object lockObject = new Object();
public static class MyRunnable implements Runnable {
@Override
public void run() {
int i;
for (i = 0; i < 1_000_000; i++) {
// TODO: Use a synchronized block to protect the counter increment
synchronized (lockObject) {
counter += 1;
}
}
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
}
}
@@ -21,14 +21,32 @@ public class TaylorSeries {
@Override
public void run() {
// TODO: Calculate the n-th term of the Taylor series for sin(x):
// term = (-1)^n * (x^(2n+1)) / (2n+1)!
// Add the term to the global sum ensuring thread-safety.
// ۱. محاسبه صورت کسر: (-1)^n * x^(2n+1)
int exponent = 2 * n + 1;
BigDecimal numerator = x.pow(exponent, mc);
if (n % 2 != 0) {
numerator = numerator.negate();
}
// TODO: Implement factorial(k) using BigDecimal
// ۲. محاسبه مخرج کسر: (2n+1)!
BigDecimal denominator = factorial(exponent);
// ۳. تقسیم صورت بر مخرج
BigDecimal term = numerator.divide(denominator, mc);
// ۴. جمع زدن ایمن با متغیر جهانی sum
synchronized (TaylorSeries.class) {
sum = sum.add(term, mc);
}
}
// پیاده‌سازی متد فاکتوریل
private BigDecimal factorial(int k) {
return BigDecimal.ZERO;
BigDecimal result = BigDecimal.ONE;
for (int i = 2; i <= k; i++) {
result = result.multiply(new BigDecimal(i), mc);
}
return result;
}
}
@@ -41,7 +59,8 @@ public class TaylorSeries {
// Submit tasks to calculate terms
for (int i = 0; i < 100; i++) {
// TODO: Submit task to the thread pool
// جایگذاری TODO سوم: ارسال تسک به ترد پول
threadPool.submit(new CalculateSin(x, i));
}
threadPool.shutdown();