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

ThreadPoolExecutor should not specify a dependency on finalization

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      Hide
      An explicit call to finalize (perhaps from subclass finalize) will no longer shutdown the pool. If you want to shut down a thread pool explicitly, you should call shutdown() !

      It's unlikely anyone is relying on a resurrected threadpool being shut down
      Show
      An explicit call to finalize (perhaps from subclass finalize) will no longer shutdown the pool. If you want to shut down a thread pool explicitly, you should call shutdown() ! It's unlikely anyone is relying on a resurrected threadpool being shut down
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Remove the (useless) finalize method body from ThreadPoolExecutor

      Problem

      The openjdk project is trying to get rid of finalizers, and ....

      we finally decided that we should remove the finalize() method's body from ThreadPoolExecutor, leaving an empty method with no throws spec for compatibility.

      The conditions for it to run are the same as the conditions under which running it has no visible effect except to change runstate status, which in turn would not be visible, except to a subclass that overrides finalize and checks, and even then, there was not a guarantee about status change, so we don't think this affects any actual usage.

      Solution

      Just make the finalize method a no-op and make minor spec adjustments.

      Specification

      Just remove the finalize method body (it's already deprecated) and in addition:

      - * <dt>Finalization</dt>
      + * <dt>Reclamation</dt>
        *
        * <dd>A pool that is no longer referenced in a program <em>AND</em>
      - * has no remaining threads will be {@code shutdown} automatically. If
      - * you would like to ensure that unreferenced pools are reclaimed even
      - * if users forget to call {@link #shutdown}, then you must arrange
      - * that unused threads eventually die, by setting appropriate
      + * has no remaining threads may be reclaimed (garbage collected)
      + * without being explicitly shutdown. You can configure a pool to
      + * allow all unused threads to eventually die by setting appropriate
        * keep-alive times, using a lower bound of zero core threads and/or
        * setting {@link #allowCoreThreadTimeOut(boolean)}.  </dd>
        *
      @@ -361,7 +357,7 @@
            * time, but need not hit each state. The transitions are:
            *
            * RUNNING -> SHUTDOWN
      -     *    On invocation of shutdown(), perhaps implicitly in finalize()
      +     *    On invocation of shutdown()
            * (RUNNING or SHUTDOWN) -> STOP
            *    On invocation of shutdownNow()
            * SHUTDOWN -> TIDYING
      +    // Override without "throws Throwable" for compatibility with subclasses
      +    // whose finalize method invokes super.finalize() (as is recommended).
      +    // Before JDK 11, finalize() had a non-empty method body.
      +
           /**
      -     * Invokes {@code shutdown} when this executor is no longer
      -     * referenced and it has no threads.
      -     *
      -     * <p>This method is invoked with privileges that are restricted by
      -     * the security context of the caller that invokes the constructor.
      -     *
      -     * @deprecated The {@code finalize} method has been deprecated.
      -     *     Subclasses that override {@code finalize} in order to perform cleanup
      -     *     should be modified to use alternative cleanup mechanisms and
      -     *     to remove the overriding {@code finalize} method.
      -     *     When overriding the {@code finalize} method, its implementation must explicitly
      -     *     ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
      -     *     See the specification for {@link Object#finalize()} for further
      -     *     information about migration options.
      +     * @implNote Previous versions of this class had a finalize method
      +     * that shut down this executor, but in this version, finalize
      +     * does nothing.
            */
           @Deprecated(since="9")
      -    protected void finalize() {
      -        SecurityManager sm = System.getSecurityManager();
      -        if (sm == null || acc == null) {
      -            shutdown();
      -        } else {
      -            PrivilegedAction<Void> pa = () -> { shutdown(); return null; };
      -            AccessController.doPrivileged(pa, acc);
      -        }
      -    }
      +    protected void finalize() {}

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dl Doug Lea
                Reporter:
                rriggs Roger Riggs
                Reviewed By:
                Alan Bateman, David Holmes, Roger Riggs
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: