1 Commits
Author SHA1 Message Date
Soroush 24125a60ec Workshop-8 2026-06-12 20:40:19 +03:30
4 changed files with 49 additions and 26 deletions
@@ -20,17 +20,18 @@ public class DeadlockPreventionWorkshop {
} }
public static void useResources(Resource r1, Resource r2) { 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.
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName()); Resource first = r1.getId() < r2.getId() ? r1 : r2;
synchronized (r1) { Resource second = r1.getId() < r2.getId() ? r2 : r1;
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.getName());
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + first.getName());
synchronized (first) {
System.out.println("+ " + Thread.currentThread().getName() + " locked " + first.getName());
try { Thread.sleep(100); } catch (InterruptedException ignored) {} try { Thread.sleep(100); } catch (InterruptedException ignored) {}
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r2.getName()); System.out.println(Thread.currentThread().getName() + " is attempting to lock " + second.getName());
synchronized (r2) { synchronized (second) {
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r2.getName()); System.out.println("+ " + Thread.currentThread().getName() + " locked " + second.getName());
System.out.println(Thread.currentThread().getName() + " using " + r1.getName() + " and " + r2.getName()); System.out.println(Thread.currentThread().getName() + " using " + r1.getName() + " and " + r2.getName());
} }
} }
@@ -6,15 +6,19 @@ import java.util.concurrent.locks.ReentrantLock;
public class LockWorkshop { public class LockWorkshop {
public static int counter = 0; public static int counter = 0;
// TODO: Create a ReentrantLock instance to protect the critical section public static final Lock lock = new ReentrantLock();
public static class MyRunnable implements Runnable { public static class MyRunnable implements Runnable {
@Override @Override
public void run() { public void run() {
int i; int i;
for (i = 0; i < 1_000_000; 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; counter += 1;
} finally {
lock.unlock();
}
} }
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i); System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
} }
@@ -3,16 +3,17 @@ package workshop.exercises;
public class SynchronizedWorkshop { public class SynchronizedWorkshop {
public static int counter = 0; public static int counter = 0;
// TODO: Define a lock object to prevent race condition private static final Object lock = new Object();
public static class MyRunnable implements Runnable { public static class MyRunnable implements Runnable {
@Override @Override
public void run() { public void run() {
int i; int i;
for (i = 0; i < 1_000_000; i++) { for (i = 0; i < 1_000_000; i++) {
// TODO: Use a synchronized block to protect the counter increment synchronized(lock) {
counter += 1; counter += 1;
} }
}
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i); System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
} }
} }
@@ -8,6 +8,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class TaylorSeries { public class TaylorSeries {
public static BigDecimal sum = BigDecimal.ZERO;
private static final Object sumLock = new Object();
public static class CalculateSin implements Runnable { public static class CalculateSin implements Runnable {
private final MathContext mc = new MathContext(1000); private final MathContext mc = new MathContext(1000);
@@ -19,29 +21,44 @@ public class TaylorSeries {
this.n = n; this.n = n;
} }
private static void accumulate(BigDecimal term) {
synchronized (sumLock) {
sum = sum.add(term);
}
}
@Override @Override
public void run() { public void run() {
// TODO: Calculate the n-th term of the Taylor series for sin(x): int exponent = (2 * n) + 1;
// term = (-1)^n * (x^(2n+1)) / (2n+1)!
// Add the term to the global sum ensuring thread-safety. BigDecimal termNumerator = x.pow(exponent, mc);
BigDecimal termDominator = factorial(exponent);
BigDecimal termValue = termNumerator.divide(termDominator, mc);
if (n % 2 != 0) {
termValue = termValue.negate();
}
accumulate(termValue);
} }
// TODO: Implement factorial(k) using BigDecimal
private BigDecimal factorial(int k) { private BigDecimal factorial(int k) {
return BigDecimal.ZERO; BigDecimal result = BigDecimal.ONE;
for (int i = 1; i <= k; i++) {
result = result.multiply(BigDecimal.valueOf(i), mc);
}
return result;
} }
} }
public static BigDecimal sum = BigDecimal.ZERO;
public static void main(String[] args) { public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(4); ExecutorService threadPool = Executors.newFixedThreadPool(4);
sum = BigDecimal.ZERO; sum = BigDecimal.ZERO;
BigDecimal x = new BigDecimal("0.01"); BigDecimal x = new BigDecimal("0.01");
// Submit tasks to calculate terms for (int i = 0; i < 126; i++) {
for (int i = 0; i < 100; i++) { threadPool.execute(new CalculateSin(x, i));
// TODO: Submit task to the thread pool
} }
threadPool.shutdown(); threadPool.shutdown();