user2977578
user2977578

Reputation: 413

Threading causes race condition

a threading question

I originally assumed the correct answer for this question is A because m2 can be partially used and call statement C while statementB is called because the two locks, "a" and "b" don't protect one another if "b" doesn't exist in the first m1.

C I think is wrong because wouldn't a synchronized method mean using the class itself as a lock, so that way nothing could enter the class if m2 was being used if it was synchronized?

Now I'm wondering if my assumption I made for C was correct. Because though it's using itself as a lock, m1 is using b as a lock to protect statementB. This means there are two monitors, ("B" and "this"), thus allowing for a clash making C the correct answer? But I'm also pretty sure A is correct as well.

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

Upvotes: 4

Views: 73

Answers (1)

Alain O'Dea
Alain O'Dea

Reputation: 21686

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

B and D are unobservable reductions in lock granularity that may degrade performance.

C is an observable change in lock granularity that could expose problematic locking by other clients. C is not a significant change in the behaviour of this code. It's a bit of a trick question. It could wind up being significant in the client code, but isn't locally significant. It preserves all of the origin local guarantees of mutual exclusion of the statements.

A is definitely the answer.

Upvotes: 1

Related Questions