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

javax.swing.TimerQueue: timer fires late when another timer starts

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8u45
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      The bug which was reported by Review ID: JI-9021974
      contains the WRONG SOURCE CODE.

      Here is the correct one



      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      import javax.swing.Timer;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      public class TimerQueueBug {

        public static void main(String[] args) throws Exception{

          for (int i = 0; i < 80; i++) {
            startShortTimer();
          }

          final boolean[] longDelay = new boolean[1];
          final Timer dyn = new Timer(100, null);
          dyn.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
              try {
                if ( longDelay[0] ) {
                  dyn.setDelay(50);
                  dyn.start();
                } else {
                  dyn.setDelay(500);
                  for ( int i = 0; i < 100; i++ ) {
                    dyn.start();
                  }
                }
                longDelay[0] = !longDelay[0];
              } catch ( Throwable ex ) {
                ex.printStackTrace();
                System.exit(99);
              }
            }
          } );
          dyn.setRepeats(true);
          dyn.start();


          while (true) {
            Thread.sleep(1);
          }

        }


        private static void startShortTimer() {
          final int delay = 100;
          Timer t = new Timer( delay, new ActionListener() {
            long nextActionExpectedAt;
            @Override
            public void actionPerformed( ActionEvent e ) {
              if (nextActionExpectedAt != 0) {
                final long diff = System.currentTimeMillis() - nextActionExpectedAt;
                if (diff > 400) {
                  System.out.println("Delay abnormal: " + diff);
                  System.exit(1);
                }
              }
              nextActionExpectedAt = System.currentTimeMillis() + delay;
            }
          } );
          t.setRepeats(true);
          t.start();
        }

      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: