Gedrox
Gedrox

Reputation: 3612

"Huge bug" in Guarded Blocks Java

IBM tutorial 5 things you didn't know about ... java.util.concurrent, Part 1 states that Java tutorial Guarded blocks example contains a "huge bug", but doesn't specify it:

Bug watch!

By the way, you're right if you noticed that Guarded Blocks contains a huge bug — what would happen if a developer synchronized on the Drop instance inside of main()?

Who's right?

If the mistake has been fixed in Java tutorial, it has happened between May 2010 and November 2011, because code looks similar in WebArchive.

Upvotes: 0

Views: 85

Answers (1)

Ted
Ted

Reputation: 48

The answers above (Artur and Gedrox) are correct--it's a violation of encapsulation to synchronize on the drop object itself, since anybody (from the outside) could also acquire that monitor, and thereby entirely disrupt the signaling mechanics.

I haven't looked at the Java Tutorial code in quite a while, but I do recall looking at it when writing the article. If they fixed it, it was after I looked at it.

As for whether this is a "huge bug" or not, I stand by the statement that it is; anything which can corrupt a synchronization scheme is to be avoided, because tracking this down would require a very high degree of proficiency with the Java sychronization system, a code path that consistently led to deadlock, and a good familiarity with all the possible permutations so that one could reason about it outside of a debugging environment. That's not a great recipe for success.

Upvotes: 2

Related Questions