为什么同步方法不包含在接口中 [英] Why synchronized method is not included in interface

查看:137
本文介绍了为什么同步方法不包含在接口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在接口中的方法上使用 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.

这篇关于为什么同步方法不包含在接口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆