为什么我们不能在接口方法声明中使用 synchronized 关键字
Why we cannot use synchronized keyword in an interface method declaration
任何人都可以通过举一个真实世界的例子来解释为什么我们不能在接口中声明同步方法。
答案很简单,synchronized是一个实现细节,不属于接口。在接口中,所有方法都没有实现细节。
文档说:
Note that a method declared in an interface must not be declared
strictfp or native or synchronized, or a compile-time error occurs,
because those keywords describe implementation properties rather than
interface properties. However, a method declared in an interface may
be implemented by a method that is declared strictfp or native or
synchronized in a class that implements the interface.
您也可以参考:What is the reason why “synchronized” is not allowed in Java 8 interface methods?
任何人都可以通过举一个真实世界的例子来解释为什么我们不能在接口中声明同步方法。
答案很简单,synchronized是一个实现细节,不属于接口。在接口中,所有方法都没有实现细节。
文档说:
Note that a method declared in an interface must not be declared strictfp or native or synchronized, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp or native or synchronized in a class that implements the interface.
您也可以参考:What is the reason why “synchronized” is not allowed in Java 8 interface methods?