Threading causes race condition

a threading question

Initially, I assumed that the correct answer for this question is A, because m2 can be partially used and call operator C when operator B is called, because the two locks "a" and "b" do not protect each other if "b" does not exist in the first m1.

C I think this is wrong because a non-synchronized method means using the class itself as a lock, so that nothing can enter the class if m2 was used if it was synchronized?

Now I'm wondering if my assumption I made for C. was correct. Because, although it uses itself as a lock, m1 uses b as a lock to protect operator B. This means there are two monitors ("B" and "this" ), which allows a collision with C to be the correct answer? But I am also sure that A is correct.

Let me know if you see any flaws in my reasoning or know the correct answer and why.

+4
source share
1 answer

I would go with A. It removes the mutual exclusion of statementB operations ; and statementC; possibly breaking it in such a way that it breaks.

B D - , .

C - , . C . . , . .

A - , , .

+1

Source: https://habr.com/ru/post/1615497/


All Articles