我目前正在阅读《Effective Java》一书,在这本书中,作者经常使用检测、通知和同步等词。然而,作为一名初级开发人员,当我在互联网上研究这些主题时,结果非常多样化。因此,我无法理解他在书中的意思。
举个例子,我正在分享一个包含这些单词的段落。
This advice may be somewhat controversial, as many programmers have grown accustomed to subclassing ordinary concrete classes to add facilities such as instrumentation, notification, and synchronization or to limit functionality
对这些术语的简要介绍将帮助我自然地理解这些主题和这本书。
最佳答案
本书中该部分的上下文是关于创建子类以通过自定义功能丰富原始类。例如,您有一个 ArrayList,并且您希望该 ArrayList 在每次向其中添加内容时向您发送通知。那么这将是一个简化的方法:
public class MyNotifyingArrayList<E> extends ArrayList<E> {
private Notifiable notifiable;
public MyNotifyingArrayList(Notifiable notifiable) {
this.notifiable = notifiable;
}
@Override
public boolean add(E e) {
boolean success = super.add(e);
if (success) {
notifiable.notify(e);
}
return success;
}
}
这将是通知
的示例。关于仪器
,我认为他考虑到了这一点:
https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html
而同步
可能意味着他想要为那些原本没有实现线程安全的类添加线程安全的能力。
关于java - 检测、通知和同步的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675034/