在 Java 8 中,我可以轻松编写:
interface Interface1 {
default void method1() {
synchronized (this) {
// Something
}
}
static void method2() {
synchronized (Interface1.class) {
// Something
}
}
}
我将获得我也可以在类中使用的完整同步语义。但是,我不能synchronized
在方法声明中使用修饰符:
interface Interface2 {
default synchronized void method1() {
// ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here
}
static synchronized void method2() {
// ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here
}
}
现在,人们可以争辩说,这两个接口的行为方式相同,只是在 on和 on 上Interface2
建立了一个合同,这比什么强一点。当然,我们也可能会争辩说,实现不应该对具体的实现状态做出任何假设,或者这样的关键字根本不会发挥作用。method1()
method2()
Interface1
default
问题:
JSR-335 专家组决定不支持synchronized
接口方法的原因是什么?