implement all TODO tasks
This commit is contained in:
@@ -20,18 +20,31 @@ 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
|
Resource first, second;
|
||||||
// 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());
|
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName());
|
||||||
synchronized (r1) {
|
|
||||||
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.getName());
|
if (r1.getId() < r2.getId()) {
|
||||||
try { Thread.sleep(100); } catch (InterruptedException ignored) {}
|
first = r1;
|
||||||
|
second = r2;
|
||||||
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r2.getName());
|
} else {
|
||||||
synchronized (r2) {
|
first = r2;
|
||||||
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r2.getName());
|
second = r1;
|
||||||
System.out.println(Thread.currentThread().getName() + " using " + r1.getName() + " and " + r2.getName());
|
}
|
||||||
|
|
||||||
|
synchronized (first) {
|
||||||
|
System.out.println("+ " + Thread.currentThread().getName() + " locked " + first.getName());
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
}
|
||||||
|
catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,22 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
public class LockWorkshop {
|
public class LockWorkshop {
|
||||||
public static int counter = 0;
|
public static int counter = 0;
|
||||||
|
|
||||||
// TODO: Create a ReentrantLock instance to protect the critical section
|
private static final ReentrantLock lock= new ReentrantLock();
|
||||||
|
|
||||||
public static class MyRunnable implements Runnable {
|
public static class MyRunnable implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
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
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,17 @@ package workshop.exercises;
|
|||||||
public class SynchronizedWorkshop {
|
public class SynchronizedWorkshop {
|
||||||
public static int counter = 0;
|
public static int counter = 0;
|
||||||
|
|
||||||
// TODO: Define a lock object to prevent race condition
|
private static final Object lock = new Object();
|
||||||
|
|
||||||
public static class MyRunnable implements Runnable {
|
public static class MyRunnable implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
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
|
|
||||||
counter += 1;
|
synchronized (lock) {
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
|
System.out.println("Increments completed by " + Thread.currentThread().getName() + ": " + i);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ import java.util.concurrent.Executors;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class TaylorSeries {
|
public class TaylorSeries {
|
||||||
|
|
||||||
|
private static final Object lock = new Object();
|
||||||
|
|
||||||
public static class CalculateSin implements Runnable {
|
public static class CalculateSin implements Runnable {
|
||||||
private final MathContext mc = new MathContext(1000);
|
private final MathContext mc = new MathContext(1000);
|
||||||
private final BigDecimal x;
|
private final BigDecimal x;
|
||||||
@@ -21,14 +23,35 @@ public class TaylorSeries {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// TODO: Calculate the n-th term of the Taylor series for sin(x):
|
BigDecimal numerator = x.pow(2*n+1, mc);
|
||||||
// term = (-1)^n * (x^(2n+1)) / (2n+1)!
|
|
||||||
// Add the term to the global sum ensuring thread-safety.
|
if (n % 2 != 0) {
|
||||||
|
|
||||||
|
numerator = numerator.negate();
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal denominator = factorial(2*n+1);
|
||||||
|
|
||||||
|
BigDecimal term = numerator.divide(denominator, mc);
|
||||||
|
|
||||||
|
synchronized (lock) {
|
||||||
|
sum = sum.add(term, mc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement factorial(k) using BigDecimal
|
|
||||||
private BigDecimal factorial(int k) {
|
private BigDecimal factorial(int k) {
|
||||||
return BigDecimal.ZERO;
|
if (k < 0) {
|
||||||
|
throw new IllegalArgumentException("k most be non-negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal result = BigDecimal.ONE;
|
||||||
|
|
||||||
|
for (int i = 2; i <=k; i++) {
|
||||||
|
|
||||||
|
result = result.multiply(BigDecimal.valueOf(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +64,8 @@ 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
|
|
||||||
|
threadPool.execute(new CalculateSin(x, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
threadPool.shutdown();
|
threadPool.shutdown();
|
||||||
|
|||||||
Reference in New Issue
Block a user