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

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

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 15
    • client-libs
    • None
    • behavioral
    • low
    • 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
    • Java API
    • SE

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

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

              Created:
              Updated:
              Resolved: