Merge pull request 'complete' (#1) from develop into master

This commit was merged in pull request #1.
This commit is contained in:
2026-07-03 07:26:21 +00:00
4 changed files with 38 additions and 27 deletions
@@ -20,18 +20,18 @@ 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());
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 " + 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 " + 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());
}
}
}
@@ -5,16 +5,19 @@ import java.util.concurrent.locks.ReentrantLock;
public class LockWorkshop {
public static int counter = 0;
private static final Lock lock = new ReentrantLock();
// TODO: Create a ReentrantLock instance to protect the critical section
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);
}
@@ -2,16 +2,16 @@ package workshop.exercises;
public class SynchronizedWorkshop {
public static int counter = 0;
public static final Object lock = new Object();
// TODO: Define a lock object to prevent race condition
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);
}
@@ -8,6 +8,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TaylorSeries {
public static BigDecimal sum = BigDecimal.ZERO;
private static final Object lock = new Object();
public static class CalculateSin implements Runnable {
private final MathContext mc = new MathContext(1000);
@@ -21,27 +23,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.
int p = 2 * n + 1;
BigDecimal sign = (n % 2 == 0) ? BigDecimal.ONE : new BigDecimal("-1");
BigDecimal num = x.pow(p, mc);
BigDecimal den = factorial(p);
BigDecimal term = sign.multiply(num, mc).divide(den, mc);
synchronized (lock) {
sum = sum.add(term, mc);
}
}
// TODO: Implement factorial(k) using BigDecimal
private BigDecimal factorial(int k) {
return BigDecimal.ZERO;
BigDecimal f = BigDecimal.ONE;
for (int i = 2; i <= k; i++) {
f = f.multiply(BigDecimal.valueOf(i));
}
return f;
}
}
public static BigDecimal sum = BigDecimal.ZERO;
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(4);
sum = BigDecimal.ZERO;
BigDecimal x = new BigDecimal("0.01");
// 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();
@@ -51,7 +60,6 @@ public class TaylorSeries {
Thread.currentThread().interrupt();
}
// Pre-calculated accurate value of sin(0.01) up to 1000 decimal places
BigDecimal accurateValue = new BigDecimal("0.009999833334166664682542438269099729038964385360169151033879" +
"1124794097345090639159659426367929614989901525182568937606738071143914781018343679925045223748779233" +
"4633395662957704288475175228160558357110105077439518716860615533070998720636987509269668842490541364" +