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

Robot.delay() catches InterruptedException and prints stacktrace to stderr

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 15
    • Component/s: client-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      Compatibility Risk is not minimal:
       - Removing "synchronized" can cause issues only if the application relied on the side effect of this synchronization. This keyword was not necessary for the implementation of this method
       - Resetting the "interrupt" state in this method may be observed by other code that is subsequently invoked with the interrupt status set
      Show
      Compatibility Risk is not minimal:  - Removing "synchronized" can cause issues only if the application relied on the side effect of this synchronization. This keyword was not necessary for the implementation of this method  - Resetting the "interrupt" state in this method may be observed by other code that is subsequently invoked with the interrupt status set
    • Interface Kind:
      Java API
    • Scope:
      SE

      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) {

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              serb Sergey Bylokhov
              Reporter:
              webbuggrp Webbug Group
              Reviewed By:
              Alan Bateman, Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: