当我在接口中的方法上使用 synchronized 时,编译器会发出错误。你能告诉我为什么会这样吗?
When I use synchronized on a method in an interface, the compiler emits an error. Can you tell me why this happens?
synchronized 无法应用于 interface ?
我试图通过Threadpool创建一个接口。 帮我在上面的代码中创建接口。
I tried to make an Interface over Threadpool in this link. Help me to make Interface in my above code.
推荐答案因为 synchronized 是一个实现细节。该方法的一个实现可能需要使该方法同步,而另一个可能不需要它。调用者不关心方法是否同步。它不是合同的一部分,它告诉方法的作用。使用哪种同步技术来完成合同是无关紧要的。
Because synchronized is an implementation detail. One implementation of the method might need to make the method synchronized, whereas another one might not need it. The caller doesn't care whether the method is synchronized or not. It's not part of the contract, which tells what the method does. Which synchronization technique, if any, is used to fulfill the contract is irrelevant.
更多推荐
为什么同步方法不包含在接口中
发布评论