JDK-6452147 : increase granularity of javax.swing.Timer
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-07-24
  • Updated: 2010-04-02
  • Resolved: 2006-09-25
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
The maximum granularity of javax.swing.Timer is roughly 25ms; a timer with delay set to 1ms generates no more than 40 ActionEvents per second.  To facilitate smoother animation, please increase the granularity to the limit supported by the underlying operating system and hardware.

JUSTIFICATION :
Animation in Swing is chunky because the dispatch thread does not run often enough.  Operating systems and hardware support higher granularity (i.e., framerate), and for that reason, native animation is smoother than Java animation.  It is valuable for a developer to produce smooth animation.  Therefore the JDK would increase in value with an increase in the granularity of javax.swing.Timer.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
An instance of javax.swing.Timer with its delay set to 1ms should produce at least 200 ActionEvents per second, because current operating systems and common hardware support it.
ACTUAL -
An instance of javax.swing.Timer with its delay set to 1ms produces no more than 40 ActionEvents per second.

---------- BEGIN SOURCE ----------

import java.awt.event.*;
import javax.swing.*;

public class ChunkyTimer extends JFrame
{
	protected TheTimerThatChunks timer;

	public ChunkyTimer()
	{
		super("Chunky Timer Demo");

		this.timer = new TheTimerThatChunks();
		this.timer.start();
	}

	protected static class TheTimerThatChunks extends javax.swing.Timer implements ActionListener
	{
		protected long start;
		protected long lastTick;

		protected int count = 0;

		public TheTimerThatChunks()
		{
			super(1, null);

			super.addActionListener(this);
		}

		public void start()
		{
			super.start();

			this.lastTick = 0;
			this.start = System.currentTimeMillis();
		}

		public void actionPerformed(ActionEvent event)
		{
			System.out.println("timer chunk: " + (System.currentTimeMillis() - this.lastTick) + "ms");

			this.lastTick = System.currentTimeMillis();

			if (++this.count > 20)
			{
				super.stop();
			}
		}
	}
	public static void main(String[] args)
	{
		try
		{
			new ChunkyTimer();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
There is no feasible workaround.

Comments
EVALUATION this bug was fixed as part of the fix for 5053997 [Swing Timer cannot handle multiple timers effectively]. closing as duplicate.
25-09-2006

EVALUATION See also related bug 5053997.
25-07-2006