views:

90

answers:

3

Is it possible to write a class such that other programmers cannot acquire a lock on an instance of the class?

Lock-abuse, if there's a term like that, can be a serious killer. unfortunately, programmers torn between the disastrous forces of delivering thread-safe code and limited knowledge of concurrency, can wreak havoc by adopting the approach of locking instances even when they're invoking operations which really don't require the instance's resources to be blocked

+6  A: 

The only way to do this is to ensure that the classes instances are not visible. For example, you could declare is as a private nested class, and make sure that the enclosing class does not leak reference instances.

Basically, if something can get hold of a reference to an instance, there is nothing to stop it from locking it.

Normally, it is sufficient to ensure that the reference to the lock object doesn't leak ... and not worry about the class visibility.

FOLLOW UP - in response to the OP's comments.

You cannot stop someone else's code from taking a lock an instance of one of your classes. But you can design your class so that this won't interfere with your classes internal synchronization. Simply arrange that your class uses a private object (even an Object instance) for synchronizing.

In the more general sense, you cannot stop application programmers from using your classes in ways that you don't like. Other examples I've heard of (here) include trying force people to override methods or provide particular constructors. Even declaring your class fields private won't stop a determined (or desperate) programmer from using reflection to get at them.

But the flip-side is that those things you are trying to prevent might actually not be stupid after all. For example, there could actually be a sound reason for an application to use your class as a lock object, notwithstanding your objection that it reduces concurrency. (And it in general it does, It is just that this may not be relevant in the particular case.)

My general feeling is that is a good idea to document the way your class is designed to be used, and design the API to encourage this. But it is unwise to try to force the issue. Ultimately it is the responsibility of the people who code against your classes to use them sensibly ... not yours.

Stephen C
while i believe your response to be the most suitable, i guess it just points to the fact that class instances can't be protected as such from locking. the class - ConcurrentHashMap - probably highlights my dilemma. Even though it has a highly sophisticated concurrency handling capability, but someone might just synchronize its instance and undo all the optimisations. In this aspect, i guess naming the class to include 'Concurrent' is also a good strategy :]
anirvan
Synchronizing on a ConcurrectHashMap only impacts code which synchronizes on it. Anyone calling the map normally will be unaffected.
Bill Michell
A: 

If a class has members that require protection from concurrent access, locking should be done internally. Otherwise, you're forcing those who use it to understand the details of its implementation when they shouldn't be able to see past its interface.

Blrfl
A: 

When creating a new instance, also create a new thread which immediately synchronizes on the instance and goes to sleep (with Thread.sleep()). Any code trying to synchronize on the instance will just deadlock, thus the developer has to rethink his approach.

Disclaimer: Don't vote me done because my suggestion is insane. I know it is. I am just answering the question. Do not actually do this!!!

Arian