Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8191849

Add a method isCleared to java.lang.ref.Reference

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE REQUEST :
      Adding a new public method:

      boolean java.lang.ref.Reference.isCleared()

      Its functionality would be to return true if this reference has been cleared and false if it has not.
      Its behaviour would be equivalente to:
      public boolean isCleared() {
          return referent == null;
      }

      No subclass of Reference would need to override this method.

      JUSTIFICATION :
      It is particularly useful for SoftReference.

      Currently, the only way to check whether it has been cleared implies invoking SoftReference.get(), which touches the timestamp.

      For some applications, such as concurrent data structures which need to search for a particular entry, it would be very important to be able to check whether a reference has been cleared without affecting the timestamp.

      For instance, if one were to implement a concurrent skip-list map, such as ConcurrentSkipListMap, but with Node extending SoftReference.
      While searching for an entry, it checks for cleared nodes to help deletion. This check would alter timestamps of many nodes, while searching for a concrete one, which is obviously not the desired behaviour. With the isCleared method, it would be straightforward to implement.

      Right now I can see no workaround. One can keep a boolean to check whether it has been programmatically cleared and also check isEnqueued, but this does not cover references which have been cleared by the GC, but not enqueued, yet.


      CUSTOMER SUBMITTED WORKAROUND :
      Incomplete workaround I've found so far:

      class MyReference<T> extends SoftReference<T> {
         boolean cleared;

         @Override
         public void clear() {
             super.clear();
             cleared = true;
         }

         public boolean isCleared() {
            // Does not cover all cases
            return cleared || isEnqueued();
         }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: