diff --git a/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java b/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java index 91ba6a8..d0f7ed2 100644 --- a/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java +++ b/src/main/java/workshop/exercises/DeadlockPreventionWorkshop.java @@ -22,7 +22,9 @@ 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. - + Resource first = r1.getId() < r2.getId() ? r1 : r2; + Resource second = 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()); diff --git a/src/main/java/workshop/exercises/LockWorkshop.java b/src/main/java/workshop/exercises/LockWorkshop.java index ec24ecf..0bbae80 100644 --- a/src/main/java/workshop/exercises/LockWorkshop.java +++ b/src/main/java/workshop/exercises/LockWorkshop.java @@ -7,14 +7,19 @@ public class LockWorkshop { public static int counter = 0; // TODO: Create a ReentrantLock instance to protect the critical section - + 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); } diff --git a/src/main/java/workshop/exercises/SynchronizedWorkshop.java b/src/main/java/workshop/exercises/SynchronizedWorkshop.java index 9259d8e..a4619c5 100644 --- a/src/main/java/workshop/exercises/SynchronizedWorkshop.java +++ b/src/main/java/workshop/exercises/SynchronizedWorkshop.java @@ -4,14 +4,16 @@ public class SynchronizedWorkshop { public static int counter = 0; // TODO: Define a lock object to prevent race condition - + 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; + synchronized (lock) { + counter += 1; + } } System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i); } diff --git a/src/main/java/workshop/exercises/TaylorSeries.java b/src/main/java/workshop/exercises/TaylorSeries.java index d63a18e..a1486da 100644 --- a/src/main/java/workshop/exercises/TaylorSeries.java +++ b/src/main/java/workshop/exercises/TaylorSeries.java @@ -24,11 +24,25 @@ public class TaylorSeries { // 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. + BigDecimal sign = (n % 2 == 0) ? BigDecimal.ONE : BigDecimal.ONE.negate(); + BigDecimal numerator = x.pow(2 * n + 1, mc); + BigDecimal denominator = factorial(2 * n + 1); + BigDecimal term = sign.multiply(numerator, mc).divide(denominator, mc); + + synchronized (TaylorSeries.class) { + sum = sum.add(term, mc); + } } // TODO: Implement factorial(k) using BigDecimal private BigDecimal factorial(int k) { - return BigDecimal.ZERO; + if (k == 0) return BigDecimal.ONE; + BigDecimal result = BigDecimal.ONE; + for (int i = 2; i <= k; i++) { + result = result.multiply(BigDecimal.valueOf(i)); + } + return result; + // return BigDecimal.ZERO; } } @@ -42,6 +56,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();