Quiz 5

Exercise on how to make classes thread-safe

We'll cover the following...

Question # 1

Is the following class thread-safe?

public class Sum {

    int count = 0;

    int sum(int... vals) {

        count++;

        int total = 0;
        for (int i = 0; i < vals.length; i++) {
            total += vals[i];
        }
        return total;
    }

    void printInvocations() {
        System.out.println(count);
    }
}
Q
A)

Yes

B)

No

Show Explanation

...