Given:
Which makes class A thread safe?
Given:
Which makes class A thread safe?
To make class A thread-safe, the methods foo and setB should both be made synchronized. Making these methods synchronized ensures that only one thread can execute either method at a time, preventing race conditions when accessing and modifying the shared variables a, b, and c.
The correct answer is B. Make foo and setB synchronized. Class A is not thread-safe because the methods foo and setB can be called concurrently by multiple threads, leading to race conditions when accessing and modifying the shared variables a, b, and c. To make class A thread-safe, both methods foo and setB should be made synchronized. This ensures that only one thread can execute either method at a time, preventing race conditions.
answer: B public class A int a = 0; int b = 0; int c = 0; First of all, class A is thread-safe because declaring variables a,b,c is not private. It is possible to change the value of them within the package directly. C. Failed Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double). Method setB does not establish a happens-before relationship, so it’s simply that there is no guarantee that the modification it causes is visible in other threads. All you get in this case is the guarantee that the it is atomic, but with no guarantees about the visibility of the (potentially) new value. So, that means that both methods should be synchronized
First of all, class A is not thread-safe because declaring variables a,b,c are not private.
B is correct!
Obviously B.
Answer: B