JDK-8230783 : Robot.delay() catches InterruptedException and prints stacktrace to stderr
  • Type: CSR
  • Component: client-libs
  • Sub-Component: java.awt
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 15
  • Submitted: 2019-09-10
  • Updated: 2019-12-14
  • Resolved: 2019-12-14
Related Reports
CSR :  
Description
Summary
-------

The `java.awt.Robot.delay(int)` method is under-specified and has surprising behavior. The method is re-specified to clearly specify how it behaves with thread interruption and the side effect of printing a stack trace is removed.

Problem
-------
- If the `delay` method is invoked with the interrupt status set, or a thread blocked in `delay` is interrupted, then it clears the interrupt status, prints a stack trace, and completes without waiting the specified time.
- Due to the method being synchronized, if several threads invoke `delay` on the same Robot instance then they will queue up and delay sequentially.

Solution
--------

 - Do not print the stack trace to stderr
 - Re-assert the "interrupt" flag if the thread was interrupted
 - Remove the synchronization to avoid queueing
 - Clarify the spec on what happens if the thread is interrupted

Specification
-------------
    src/java.desktop/share/classes/java/awt/Robot.java

         /**
          * Sleeps for the specified time.
    -     * To catch any {@code InterruptedException}s that occur,
    -     * {@code Thread.sleep()} may be used instead.
    +     * <p>
    +     * If the invoking thread is interrupted while waiting, then it will
    +     * return immediately with the interrupt status set. This method
    +     * does nothing if invoked with the interrupt status set.
          *........
          *........
          */
    -    public synchronized void delay(int ms) {
    +    public void delay(int ms) {
Comments
Approved for 15 is a release note is written.
14-12-2019