answer: CD
Thread safety is the avoidance of data races
When the result of multiple threads executing a critical section may differ depending on the sequence in which the threads execute
The reason of that could be visibility and atomicity problem.
Note that atomicity is very different to visibility. After atomic operation we can see another value without the happens-before relationship (see Java Memory Model).
To avoid data inconsistency we can use synchronized statements or atomic operator like AtomicInteger.
B Failed
the use of volatile variables are limited, in non atomic operations (auto increment)
E. Failed
by definition synchronized statement
synchronized(sync_object) {}
Option C replaces line 3 with a synchronized block that synchronizes on the test object. This ensures that only one thread can execute the code within the synchronized block at a time, preventing race conditions when multiple threads try to increment the count variable simultaneously.
Option D replaces line 1 with an AtomicInteger variable, which provides atomic operations for updating its value. The incrementAndGet method is then used on line 3 to atomically increment the value of the count variable. This also prevents race conditions when multiple threads try to increment the count variable simultaneously.
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
5 months, 1 week agod7bb0b2
1 year agod7bb0b2
1 year agoStavok
1 year, 6 months agoMukes877
1 year, 8 months agoRP384
1 year, 11 months ago