为什么同步方法不包含在接口中

编程入门 行业动态 更新时间:2024-10-24 20:13:31
本文介绍了为什么同步方法不包含在接口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

为什么同步方法不包含在接口中

本文发布于:2023-06-07 15:24:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不包含   接口   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!