Name: _____________________________ 6 Digit StudentID: ___ ___ ___ ___ ___ ___

Worksheet 27: Omega

Is there a data race in the code below? ________

If there is a data race, encircle all of the lines of code which contribute to the data race.

public static double hypotenuse(double a, double b) {
    Lock lock = new ReentrantLock();
    double[] sum = {0.0};
    Future<Void> future = void_fork(() -> {
        lock.lock();
        try {
            sum[0] += a * a;
        } finally {
            lock.unlock();
        }
    });
    sum[0] += b * b;
    join(future);
    return Math.sqrt(sum[0]);
}

Spoiler, there is a data race. Implement a race free version below:

public static double hypotenuse(double a, double b)

















Post Lecture

Synthesize today's class session

 

 

What is unclear?