-
CSR
-
Resolution: Approved
-
P3
-
None
-
behavioral
-
low
-
-
Java API
-
SE
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.
- csr of
-
JDK-8186265 Make toString() methods of "task" objects more useful
- Resolved