Search code examples
javanotificationssynchronizationinstrumentation

Meanings of Instrumentation, Notification and Synchronization


I am currently reading the "effective Java" book and in this book, the writer uses frequently the words instrumentation, notification and synchronization. However, as a Junior Developer, when I research these topics on internet, the results are very diverse. Because of this reason, I cannot comprehend what he means in his book.

As an example, I am sharing a paragraph, which contains these words.

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

Brief introduction to these terminologies would help me a lot understand these topics and the book naturally.


Solution

  • The context of the section in the book is about creating subclasses to enrich the original class with custom functionality. For instance, you have an ArrayList and you'd like that ArrayList to send you a notification each time something is added to it. Then this would be a simplified approach:

    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;
      }
    }
    

    This would be an example for notification. Regarding instrumentation I assume he had this in mind:

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

    And synchronization probably means that he wants to add thread-safe abilities to classes that have not originally been implemented to be thread-safe.