When I was looking at my simple examples again that demonstrate how benign code can be transformed into code that breaks concurrency invariants and deadlocks, I realized that the annotations I had for dealing with owning locks, namely OnlySynchronized\*
and NotSynchronized\*
are not enough. They only check if the current thread owns or does not own the lock. I need to know if any thread owns a lock.
I’ve now added a two groups of annotations, AnySynchronized\*
and NoneSynchronized\*
, which express the invariant “any thread owns a lock” and “no thread owns a lock”, respectively.
The implementation is a bit of a hack, as it spawns another thread that may be doomed, but it kind of works, and with a higher level of instrumentation, i.e. instrumenting the rt.jar and probably all monitorenter
and monitorexit
calls, I could do a better and more definite job, but I’m trying to keep it lightweight here.
The difficulties arise mostly because of Java’s very limited model of object monitors and monitorenter
and monitorexit
. Allen Holub proposed improvements to synchronized that I can wholeheartedly agree with, and Sun made some improvements with Java 1.5 and the java.lang.concurrent package, particularly the ReentrantLock
class.
But I can always make improvements later. Right now, I need to focus on advancing my thesis (sadly, I’ll probably miss the deadline), but I keep running into things I should add, like during the last week.