Why can static and default interface methods not be synchronized?
People say that synchronized is an implementation detail. Well, strictfp is an implementation detail as well, but that doesn't prevent allowing strictfp on static and default interface methods.
A default method is inherited and it may be quite convenient to have it already synchronized if the class implementing the interface does not override the default method.
I have a guess that synchronized (as well as strictfp) is not inherited (am I right here?), but that does not explain why strictfp is allowed for static and default interface methods either.
static methods can be synchronized. But you have one lock per class. when the java class is loaded coresponding java.
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. A static method is a method that is associated with the class in which it is defined rather than with any object.
A synchronized block of code can only be executed by one thread at a time. Synchronization in Java is basically an implementation of monitors . When synchronizing a non static method, the monitor belongs to the instance. When synchronizing on a static method , the monitor belongs to the class.
The strictfp
keyword ensures that your floating-point operations are consistent across all platforms. stricftp
then becomes a guarantee of the JVM that your floating-point operations are going to be the same on all platforms, and that one can expect that floating-point operations are portable and consistent throughout.
A method marked synchronized
is actually an implementation detail, and cannot be dictated or controlled by any one interface for any of its implementations. It was intentionally excluded from default methods, as explained by Brian Goetz, due to them being inherently dangerous (emphasis mine):
...So, why are they dangerous? Synchronization is about locking. Locking is about coordinating shared access to mutable state. Each object should have a synchronization policy that determines which locks guard which state variables. (See Java Concurrency in Practice, section 2.4.)
...It is the class that owns the state that gets to determine that object's synchronization policy. But interfaces do not own the state of the objects into which they are mixed in. So using a synchronized method in an interface assumes a particular synchronization policy, but one which you have no reasonable basis for assuming, so it might well be the case that the use of synchronization provides no additional thread safety whatsoever (you might be synchronizing on the wrong lock).
I think this is because if it were allowed:
interface I {
static synchronized void x() {
}
}
class C implements I {
synchronized static void y() {
}
}
then two different threads could enter C.y() and C.x() because x() is synchronized on I.class and y() on C.class. It is only my guess
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With