We are using the ScheduledExecutorService.scheduleAtFixedRate
to perform an I/O Task every 100 ms. (The actual I/O Operation is performed by a third party library and we don't know exactly what is happening inside.)
Sometimes keeping up with the 100 ms intervals has some issues and we fall back to 500 ms. Since it is I/O, we weren't surprised, but we observed a strange behavior:
If a specific Thread runs, we are matching the 100 ms. If this thread is not running, we are falling back to 500 ms.
That specific Threads' run-method looks like this:
while(active){
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
//some more stuff
}
Basically, when we use a short delay like 5 ms we get a better performance within the library. When we use longer delays like 1000 ms, performance is significantly worse.
It also seems to be platform-specific since we were not able to reproduce the problem (Java 8, Windows 10).
All that we know is that it's definitely the short sleep()-call causing the improvement since we could fix the issue by running a dummy thread just sleeping in short intervals.
Any explanation would be helpful to understand what is happening :-)
--- edit
Even more interesting: if we are adding not one but two new Threads only for short sleep intervals, it adds a little bit of performance. Not as significant as the first thread, but still like 20%.
--- edit 2
The System where we can observe the behavior: Intel Atom E3845 Windows 10
The System where we can't reproduce it: Intel i7-5820K Windows 10
We can't look up the source code but the library seems to run single-threaded (no new threads created) and creates a Socket connection.