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
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,
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
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
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
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.