Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.
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
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.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
16 hours, 35 minutes agoASPushkin
16 hours, 34 minutes agod7bb0b2
6 months agoOmnisumem
10 months agoStavok
11 months, 3 weeks agotmuralimanohar
1 year ago