I need to write some code to stress-test RS232 communication using rxtx (note: this question is NOT about serial communications, so please don’t go there), and I would like to periodically send bytes using some kind of timer task.
Are there any kind of guidelines online about what kind of timer class to use for various applications? I’m not sure whether I should use javax.swing.Timer or java.util.Timer or java.util.concurrent.ScheduledExecutorService or something else.
Also at what frequency do they start to break down? I would guess that it’s along the lines of the operating system timeslice (forget what it is in Windows, 1msec-10msec seems about what I remember). I want to run at the fastest frequency that will not give me problems on my PC; I can always spit out more bytes out my serial port at a time, I’d just like to do it in as much of a periodic fashion as I can.
Don’t use the Swing timer; that’s a kluge from early Swing days.
The ScheduledExecutorService is the more general and configurable of the two remaining contenders. It accepts nanosecond precision, but of course cannot provide any better than milisecond accuracy depending on platform. You’d need a real-time JVM for nanosecond accuracy.