complete advanced multithreading exercises #1

Merged
peyman merged 1 commits from develope into master 2026-07-24 08:36:27 +00:00
5 changed files with 33 additions and 4 deletions
@@ -22,7 +22,9 @@ 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 // 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. // Hint: Compare resource IDs and always synchronize on the resource with the smaller ID first.
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()); System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName());
synchronized (r1) { synchronized (r1) {
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.getName()); System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.getName());
@@ -7,6 +7,7 @@ public class LockWorkshop {
public static int counter = 0; public static int counter = 0;
// TODO: Create a ReentrantLock instance to protect the critical section // TODO: Create a ReentrantLock instance to protect the critical section
private static final Lock lock = new ReentrantLock();
public static class MyRunnable implements Runnable { public static class MyRunnable implements Runnable {
@Override @Override
@@ -14,7 +15,13 @@ public class LockWorkshop {
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 // 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); System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
} }
@@ -4,6 +4,7 @@ public class SynchronizedWorkshop {
public static int counter = 0; public static int counter = 0;
// TODO: Define a lock object to prevent race condition // TODO: Define a lock object to prevent race condition
private static final Object lockObject = new Object();
public static class MyRunnable implements Runnable { public static class MyRunnable implements Runnable {
@Override @Override
@@ -11,7 +12,9 @@ public class SynchronizedWorkshop {
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 // TODO: Use a synchronized block to protect the counter increment
counter += 1; synchronized (lockObject){
counter += 1;
}
} }
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i); System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
} }
@@ -24,11 +24,27 @@ public class TaylorSeries {
// TODO: Calculate the n-th term of the Taylor series for sin(x): // TODO: Calculate the n-th term of the Taylor series for sin(x):
// term = (-1)^n * (x^(2n+1)) / (2n+1)! // term = (-1)^n * (x^(2n+1)) / (2n+1)!
// Add the term to the global sum ensuring thread-safety. // Add the term to the global sum ensuring thread-safety.
int power = 2 * n + 1;
BigDecimal num = x.pow(power, mc);
BigDecimal den = factorial(power);
BigDecimal term = num.divide(den, mc);
if (n % 2 != 0){
term = term.negate();
}
synchronized (TaylorSeries.class){
sum = sum.add(term, mc);
}
} }
// TODO: Implement factorial(k) using BigDecimal // 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 = 2; i <= k; i++) {
result = result.multiply(new BigDecimal(i), mc);
}
return result;
} }
} }
@@ -42,6 +58,7 @@ public class TaylorSeries {
// Submit tasks to calculate terms // Submit tasks to calculate terms
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
// TODO: Submit task to the thread pool // TODO: Submit task to the thread pool
threadPool.submit(new CalculateSin(x, i));
} }
threadPool.shutdown(); threadPool.shutdown();
BIN
View File
Binary file not shown.