Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4712166086 |
@@ -21,7 +21,14 @@ 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;
|
||||
Resource second = r2;
|
||||
if (r1.getId() > r2.getId()) {
|
||||
first = r2;
|
||||
second = r1;
|
||||
}
|
||||
r1 = first;
|
||||
r2 = second;
|
||||
|
||||
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName());
|
||||
synchronized (r1) {
|
||||
|
||||
@@ -7,6 +7,7 @@ public class LockWorkshop {
|
||||
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 {
|
||||
@Override
|
||||
@@ -14,8 +15,13 @@ public class LockWorkshop {
|
||||
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
|
||||
lock.lock();
|
||||
try {
|
||||
counter += 1;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
|
||||
}
|
||||
}
|
||||
@@ -35,3 +41,4 @@ public class LockWorkshop {
|
||||
System.out.println("Final counter value (expected 2000000): " + counter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ public class SynchronizedWorkshop {
|
||||
public static int counter = 0;
|
||||
|
||||
// TODO: Define a lock object to prevent race condition
|
||||
public static final Object lock = new Object();
|
||||
|
||||
public static class MyRunnable implements Runnable {
|
||||
@Override
|
||||
@@ -11,6 +12,7 @@ public class SynchronizedWorkshop {
|
||||
int i;
|
||||
for (i = 0; i < 1_000_000; i++) {
|
||||
// TODO: Use a synchronized block to protect the counter increment
|
||||
synchronized (lock) {
|
||||
counter += 1;
|
||||
}
|
||||
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
|
||||
@@ -32,3 +34,4 @@ public class SynchronizedWorkshop {
|
||||
System.out.println("Final counter value (expected 2000000): " + counter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,16 +22,28 @@ 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.
|
||||
BigDecimal numerator = x.pow(2 * n + 1, mc);
|
||||
BigDecimal denominator = factorial(2 * n + 1);
|
||||
|
||||
BigDecimal term = numerator.divide(denominator, mc);
|
||||
|
||||
if (n % 2 != 0) {
|
||||
term = term.negate();
|
||||
}
|
||||
|
||||
synchronized (TaylorSeries.class) {
|
||||
sum = sum.add(term, mc);
|
||||
}
|
||||
}
|
||||
// TODO: Implement factorial(k) using BigDecimal
|
||||
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));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static BigDecimal sum = BigDecimal.ZERO;
|
||||
|
||||
public static void main (String[]args){
|
||||
@@ -42,6 +54,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();
|
||||
|
||||
Reference in New Issue
Block a user