diff --git a/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java b/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java index 91ba6a8..611ae26 100644 --- a/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java +++ b/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java @@ -20,18 +20,38 @@ 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. - - System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName()); - synchronized (r1) { - System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.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()); + + // Always lock resources in ascending order of ID + Resource first = r1; + Resource second = r2; + + if (r1.getId() > r2.getId()) { + first = r2; + second = r1; + } + + 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) { + } + + System.out.println(Thread.currentThread().getName() + + " is attempting to lock " + second.getName()); + + synchronized (second) { + System.out.println("+ " + Thread.currentThread().getName() + + " locked " + second.getName()); + + System.out.println(Thread.currentThread().getName() + + " using " + first.getName() + + " and " + second.getName()); } } } @@ -40,11 +60,12 @@ public class DeadlockPreventionWorkshop { Resource resA = new Resource(1, "ResourceA"); Resource resB = new Resource(2, "ResourceB"); - // Thread 1 locks A then B; Thread 2 locks B then A (causing deadlock if unordered) + // Thread 1 requests A then B + // Thread 2 requests B then A Thread t1 = new Thread(() -> useResources(resA, resB), "Thread-1"); Thread t2 = new Thread(() -> useResources(resB, resA), "Thread-2"); t1.start(); t2.start(); } -} +} \ No newline at end of file diff --git a/src/main/java/workshop/exercises/LockWorkshop.java b/src/main/java/workshop/exercises/LockWorkshop.java index ec24ecf..b065285 100644 --- a/src/main/java/workshop/exercises/LockWorkshop.java +++ b/src/main/java/workshop/exercises/LockWorkshop.java @@ -5,16 +5,21 @@ import java.util.concurrent.locks.ReentrantLock; public class LockWorkshop { public static int counter = 0; - - // TODO: Create a ReentrantLock instance to protect the critical section + + // Lock protecting the shared counter + private static final Lock lock = new 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 - counter += 1; + lock.lock(); + try { + counter += 1; + } finally { + lock.unlock(); + } } System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i); } @@ -25,13 +30,13 @@ public class LockWorkshop { Thread thread1 = new Thread(new MyRunnable(), "Thread-1"); Thread thread2 = new Thread(new MyRunnable(), "Thread-2"); - + thread1.start(); thread2.start(); thread1.join(); thread2.join(); - + System.out.println("Final counter value (expected 2000000): " + counter); } -} +} \ No newline at end of file diff --git a/src/main/java/workshop/exercises/SynchronizedWorkshop.java b/src/main/java/workshop/exercises/SynchronizedWorkshop.java index 9259d8e..a619f5e 100644 --- a/src/main/java/workshop/exercises/SynchronizedWorkshop.java +++ b/src/main/java/workshop/exercises/SynchronizedWorkshop.java @@ -2,16 +2,19 @@ package workshop.exercises; public class SynchronizedWorkshop { public static int counter = 0; - - // TODO: Define a lock object to prevent race condition + + // Lock object used for synchronization + private static final Object lock = 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 - counter += 1; + // Protect the shared counter + synchronized (lock) { + counter += 1; + } } System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i); } @@ -22,13 +25,13 @@ public class SynchronizedWorkshop { Thread thread1 = new Thread(new MyRunnable(), "Thread-1"); Thread thread2 = new Thread(new MyRunnable(), "Thread-2"); - + thread1.start(); thread2.start(); thread1.join(); thread2.join(); - + System.out.println("Final counter value (expected 2000000): " + counter); } -} +} \ No newline at end of file diff --git a/src/main/java/workshop/exercises/TaylorSeries.java b/src/main/java/workshop/exercises/TaylorSeries.java index d63a18e..6a5e4ad 100644 --- a/src/main/java/workshop/exercises/TaylorSeries.java +++ b/src/main/java/workshop/exercises/TaylorSeries.java @@ -8,7 +8,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class TaylorSeries { - + public static class CalculateSin implements Runnable { private final MathContext mc = new MathContext(1000); private final BigDecimal x; @@ -21,14 +21,34 @@ 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. + // term = (-1)^n * x^(2n+1) / (2n+1)! + + int exponent = 2 * n + 1; + + BigDecimal numerator = x.pow(exponent, mc); + BigDecimal denominator = factorial(exponent); + + BigDecimal term = numerator.divide(denominator, mc); + + if (n % 2 == 1) { + term = term.negate(); + } + + // Thread-safe update of the global sum + synchronized (TaylorSeries.class) { + sum = sum.add(term, mc); + } } - // TODO: Implement factorial(k) using BigDecimal + // Computes k! private BigDecimal factorial(int k) { - return BigDecimal.ZERO; + BigDecimal result = BigDecimal.ONE; + + for (int i = 2; i <= k; i++) { + result = result.multiply(BigDecimal.valueOf(i), mc); + } + + return result; } } @@ -41,7 +61,7 @@ public class TaylorSeries { // Submit tasks to calculate terms for (int i = 0; i < 100; i++) { - // TODO: Submit task to the thread pool + threadPool.submit(new CalculateSin(x, i)); } threadPool.shutdown(); @@ -72,4 +92,4 @@ public class TaylorSeries { System.out.println("Accurate Value: " + accurateValue.toPlainString()); System.out.println("Difference: " + accurateValue.subtract(sum).abs().toPlainString()); } -} +} \ No newline at end of file