在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
}
}
现在,可以认为两个接口的行为方式相同,只是Interface2在method1()和method2()上建立了一个合约,这比Interface1要强一些。当然,我们也可能认为默认实现不应该对具体实现状态做出任何假设,或者这样的关键字不会简单地增加其权重。
题:
JSR-335专家组决定不支持同步接口方法的原因是什么?
注意,在这个问题急切关闭之前:适合Stack Overflow的Q& A格式:我只寻找权威的引文,而不是猜测。这个问题的权威答案可能会帮助许多未来的游客这个问题,所以请不要急切地关闭它。