检测、通知和同步的含义

Meanings of Instrumentation, Notification and Synchronization

我目前正在阅读 "effective Java" 这本书,在这本书中,作者经常使用 instrumentationnotification 等词同步。然而,作为一名初级开发人员,当我在互联网上研究这些主题时,结果却五花八门。由于这个原因,我无法理解他书中的意思。

举个例子,我正在分享一个包含这些词的段落。

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

对这些术语的简要介绍将帮助我自然而然地理解这些主题和本书。

本书中该部分的上下文是关于创建子class 以使用自定义功能丰富原始 class。例如,您有一个 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;
  }
}

这将是 notification 的示例。关于 instrumentation 我猜他是这么想的:

https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

synchronization大概的意思是他想给class原本没有实现线程安全的es增加线程安全的能力。