How to synchronize inside an interface default method without using this?

1.8k views Asked by At

I have a number of default methods in interfaces that need synchronization and it seems that only this is available:

default void addUniqueColumns(List<String> names) {
    synchronized (this) {
        ... do something
    }
}

The problem is, I want to synchronize on a private lock instead of this for better control:

default void addUniqueColumns(List<String> names) {
    synchronized (lock) {  // how to get a private lock in a default method??
        ... do something
    }
}

Solutions? Clever workarounds? Or just live with it :) !

3

There are 3 answers

6
Sergey Kalinichenko On BEST ANSWER

You can put the lock object into a pubic static field of a package-visible class, letting all your default methods share the lock. The lock remains visible inside your library, but since classes with default access are not visible outside your library, the lock would be private to the users of your interface outside your library:

class LockHolder { // Package private class
    public static Object LOCK = new Object();
}

public interface ColumnCollection {
    default void addUniqueColumns(List<String> names) {
        synchronized (LockHolder.LOCK) {
            ... do something
        }
    }
}

As far as your library is concerned as a whole, this trick gives you the same advantages as using a private lock object does compared to synchronizing on this, because it prevents malicious code written by outsiders from accessing your lock. Of course the lock can be grabbed by any part of your library.

3
The Coordinator On

For the heck of it (and some entertainment value) let's see what might be feasable ...

I put the lock object into a static field of a package-visible class, letting all my default methods share the lock. A lock provider provides instances their own lock on-demand. The lock is removed from the collection when the instance is garbage collected.

The lock provider creates a lock the first time it is requested from an instance and then returns the same lock thereafter. It looks like this:

final class LockProvider {

    private static final WeakHashMap<Widget,Object> widgetLocks = new WeakHashMap<>();

    static Object obtainLock(Widget w) {
        synchronized (widgetLocks) {            
             return locks.computeIfAbsent(w, x -> new Object());
        }
    }

}

And now the default interface method looks like this:

public interface Widget{

    default void addSomething(List<String> names) {
        synchronized (LockProvider.obtainLock(this)) {
            ... do something
        }
    }

}

One weakness of this is that the WeakHashMap uses Object.hashcode() and Object.equals(). Another is that, although fast, it is not super-high-performance. Although this way of doiung it seems clever ... any method that requires synchronization on a private lock would be better designed in another way.

[UPDATED]

What I did in the end was:

1) create default methods:

public interface Widget{

    default void addSomething(List<String> something) {
        ... do something 
    }
}

2) Then created both regular and thread-safe implementations

public class WidgetImpl implements Widget{
    ...
}

// Threadsafe version
public class WidgetThreadsafeImpl implements Widget{

    private final Object lock = new Object(); 

    public void addSomething(List<String> something) {
        synchronized(lock){
            super.addSomething(something);
        }
   }

}

The default method provides an algorithm and the implementations can provide the thread-safe or non-thread-safe implementations.

2
ksasq On

You could add a getLock() method to your interface and have each implementor return the object to lock over.