Synchronized Methods Harder to Protect

I’m working with the simple examples for my thesis again, and I realized another interesting thing: Synchronized methods are harder to protect than non-synchronized methods.

The example that I’m using is this:

import edu.rice.cs.cunit.threadCheck.SuppressSubtypingWarning;
import edu.rice.cs.cunit.threadCheck.predicates.NoneSynchronizedField;
public class BenignCall extends Benign {
private static Object o = new Object() {
public synchronized String toString() { return "foo"; }
public static void main(String[] args) {
final Benign b = new BenignCall();
synchronized(o) {
Thread aux = new Thread(new Runnable() {
public void run() {, o);
try { aux.join(); } catch(InterruptedException e) { }
//,o); would have worked

I’m using a @NoneSynchronizedField annotation to express the invariant that no thread may hold the lock of the o field at the time the method is invoked.

I just looked at it again and thought it would be much more natural to state that the lock of this may not be held when the overloaded toString() method is called:

public synchronized String toString() { return "foo"; }

But I realized this wouldn’t work. With synchronized methods, Java grabs the lock automatically, behind the scenes, and by the time code in the method is executed, the lock is already held. Or not, in this example, the program may deadlock.

I think with this in mind, it may be a good idea to convert all synchronized methods to methods containing synchronized blocks, something that I already do quite often.

Update: I now have a parameter “convert-sync-methods” that can be passed to the Thread Checker instrumentation strategy that converts synchronized methods to methods containing synchronized blocks. So now the @NoneSynchronizedField annotation can be used there.


About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in Concurrent Unit Testing, MS Thesis. Bookmark the permalink.

Leave a Reply