Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51c5f629ed |
@@ -17,13 +17,13 @@
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<version>5.10.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<version>5.9.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -20,17 +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.
|
||||
|
||||
System.out.println(Thread.currentThread().getName() + " is attempting to lock " + r1.getName());
|
||||
synchronized (r1) {
|
||||
System.out.println("+ " + Thread.currentThread().getName() + " locked " + r1.getName());
|
||||
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 " + 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() + " is attempting to lock " + second.getName());
|
||||
synchronized (second) {
|
||||
System.out.println("+ " + Thread.currentThread().getName() + " locked " + second.getName());
|
||||
System.out.println(Thread.currentThread().getName() + " using " + r1.getName() + " and " + r2.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,20 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
public class LockWorkshop {
|
||||
public static int counter = 0;
|
||||
|
||||
// TODO: Create a ReentrantLock instance to protect the critical section
|
||||
private 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);
|
||||
}
|
||||
|
||||
@@ -3,15 +3,16 @@ package workshop.exercises;
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -19,29 +19,44 @@ public class TaylorSeries {
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
private static void accumulate(BigDecimal term){
|
||||
synchronized (sumLock){
|
||||
sum = sum.add(term);
|
||||
}
|
||||
}
|
||||
|
||||
@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 exponent = 2*n+1;
|
||||
|
||||
BigDecimal termNumerator = x.pow(exponent, mc);
|
||||
BigDecimal termDenominator = factorial(exponent);
|
||||
BigDecimal termValue = termNumerator.divide(termDenominator);
|
||||
|
||||
if (n % 2 != 0){
|
||||
termValue = termValue.negate();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Implement factorial(k) using BigDecimal
|
||||
private BigDecimal factorial(int k) {
|
||||
return BigDecimal.ZERO;
|
||||
BigDecimal reault = BigDecimal.ONE;
|
||||
for (int i = 0; i <= k ; i++) {
|
||||
reault = reault.multiply(BigDecimal.valueOf(i), mc);
|
||||
}
|
||||
return reault;
|
||||
}
|
||||
}
|
||||
|
||||
public static BigDecimal sum = BigDecimal.ZERO;
|
||||
private static final Object sumLock = new Object();
|
||||
|
||||
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.execute(new CalculateSin(x, i));
|
||||
}
|
||||
|
||||
threadPool.shutdown();
|
||||
|
||||
Reference in New Issue
Block a user