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

Make toString() methods of "task" objects more useful

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      Although we are changing the behavior of several toString methods, we propose not changing the spec of any method to reflect that (toString methods are supposed to provide useful information for humans!), except for FutureTask, where we align with CompletableFuture, but we use @implSpec so that existing subclasses that override toString stay in spec.

      All task wrapper classes will have a toString method with body
      ```
      return super.toString() + "[Wrapped task = " + task + "]";
      ```

      FutureTask will have a toString method with body like
      ```
      return super.toString() + "[" + <STATUS> + "]"
      ```

      Yes, people who are depending on the current less helpful output of toString may be broken (but the output will continue to start with super.toString(); we're merely appending.
      Show
      Although we are changing the behavior of several toString methods, we propose not changing the spec of any method to reflect that (toString methods are supposed to provide useful information for humans!), except for FutureTask, where we align with CompletableFuture, but we use @implSpec so that existing subclasses that override toString stay in spec. All task wrapper classes will have a toString method with body ``` return super.toString() + "[Wrapped task = " + task + "]"; ``` FutureTask will have a toString method with body like ``` return super.toString() + "[" + <STATUS> + "]" ``` Yes, people who are depending on the current less helpful output of toString may be broken (but the output will continue to start with super.toString(); we're merely appending.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Make toString() methods of "task" objects more useful.

      Problem

      People are often staring at output of toString for "task" objects (Runnables, Callables, Futures) when diagnosing app failures. toString should be more helpful, without adding the risk of unbounded computation by printing result objects.

      Solution

      CompletableFuture already has a useful toString method, giving the current status. We should add toString of any exception thrown. FutureTask should have a similar toString method.

      Adapters such as Executors.callable should include the wrapped object's toString method, making them better replacements for the wrapped object.

      • Executors.callable(Runnable)
      • Executors.callable(Runnable, Object)
      • Executors.privilegedCallable
      • Executors.privilegedCallableUsingCurrentClassLoader
      • ForkJoinTask.adapt

      http://cr.openjdk.java.net/~martin/webrevs/openjdk10/jsr166-integration/toString-moredata/

      http://markmail.org/thread/r46dnoo73lawt26t

      Specification

      +    /**
      +     * Returns a string representation of this FutureTask.
      +     *
      +     * @implSpec
      +     * The default implementation returns a string identifying this
      +     * FutureTask, as well as its completion state.  The state, in
      +     * brackets, contains one of the strings {@code "Completed Normally"},
      +     * {@code "Completed Exceptionally"}, {@code "Cancelled"}, or {@code
      +     * "Not completed"}.
      +     *
      +     * @return a string representation of this FutureTask
      +     */
      +    public String toString() {

      Although we are changing the behavior of several toString methods, we propose not changing the spec of any method to reflect that (toString methods are supposed to provide useful information for humans!), except for FutureTask, where we align with CompletableFuture, but we use @implSpec so that existing subclasses that override toString stay in spec.

      All task wrapper classes will have a toString method with body

      return super.toString() + "[Wrapped task = " + task + "]";

      FutureTask will have a toString method with body like

      return super.toString() + "[" + <STATUS> + "]"

      Yes, people who are depending on the current less helpful output of toString may be broken (but the output will continue to start with super.toString(); we're merely appending.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                martin Martin Buchholz
                Reporter:
                martin Martin Buchholz
                Reviewed By:
                David Holmes, Doug Lea, Roger Riggs
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: