high frequency loop
<p>How can I make a loop that executes about 10000 times per second with regular(!) intervals?</p>

<p>(duplicate of <a href="http://stackoverflow.com/questions/7691257/continuous-data-acquisition-from-parallel-port-in-java">continuous data acquisition from parallel port in Java</a> but that one's hard-to-find and isn't answered and very old)</p>

<p>I looked at <code>Thread.sleep(long millis)</code> and <code>Thread.sleep(long millis, int nanos)</code> but Oracle's J2SE virtual machine on Windows always sleeps 1 millesecond longer than what I specify. Besides, the nanos field seems to be rounded up to the next whole millisecond (verified, this is hardcoded in the source code of Thread.java).</p>

<p>Experimantal results:</p>

<ul>
<li><code>Thread.sleep(0)</code> sleeps not (100% cpu)</li>
<li><code>Thread.sleep(1)</code> sleeps 1.95ms on average</li>
<li><code>Thread.sleep(2)</code> sleeps 2.95ms on average</li>
<li><code>Thread.sleep(0, 0)</code> sleeps not (100% cpu)</li>
<li><code>Thread.sleep(0, 1)</code> sleeps 1.95ms on average</li>
</ul>

<p>So how can I make a loop that iterates more than even 500 times per second with regular intervals?</p>

<p>Edit: I loosen the 'regular' requirement a little. It is not a big problem if one delay is like 4 times shorter than another delay (ie. jitter is not a problem), as long as the longest delay is deterministic and below 0.1ms. (which isn't the case with <code>ScheduledExecutorService</code>)</p>
