Motivation
Few weeks ago I was searching for a source of a bug in one of our applications. The QA team suspected that slow computer could be a reason for this. You know that - developers have Core i7 CPU with 8 GB RAM (at least some of us :-) ) and never realize, that users can have slow machine, which reacts differently.And for that tiny moment you would need slow computer, just to see how it works and whether you can break it. But how to achieve this? The QA team does that by creating a virtual machine and setting it very low resources, so it acts very slow. Another option is to use some kind of CPU killer – those are great pieces of software, doing exactly what it sounds – killing your CPU.
There are tons of complete solutions out there, but I didn’t want to deal with the licensing and our IT stuff yelling at me, what is that software that I installed by myself again. So I decided to break the rule “Don’t reinvent the wheel” again.
Requirements
At first, here is a list of features, you would expect of a CPU killer:- It works :-) (not that easy actually)
- Ability to set the load factor
- Nice GUI, plus console client
Implementation
But how to make your CPU busy? Well, let it compute something, my favorite class for this is java.lang.Math class with methods for mathematical functions like sin or log10 with combination of some random number. Run the computation in an endless loop and your CPU fan will love you :-)The second point was little bit trickier; you need to spend only some time doing the computation to be able to set the load factor. The idea was to measure the time spent by calculation and use it to calculate the idle time. With correct idle time you can ensure pretty precise load factor. Sounds easy right? But the problem is that the thread has to sleep for really short period of time, milliseconds are not enough.
java.lang.Thread class has a method sleep(long millis, int nanos) which looks great, but simply doesn’t work. Next blog will be dedicated to this method, so I won't go to any details here. I just had to use method sleep(long millis).
And now what? With 1 millisecond as the smallest idle time, you need to think about different solution. And here it is: every cycle sum the time spent for the computation and when the total time corresponds with 1ms idle time, put the current thread to sleep for one millisecond. And guess what? It works :-)
The only problem is, that with sleep time 1 millisecond, the load become sort of unstable with lower load factor (let’s say 40% and less). The solution is easy – with more CPU cores, use only some cores and set them to higher load factor and the overall load is stable.
Last step, the GUI, was the easiest one. With JavaFX as a platform and JavaFX Scene Builder as a graphics designer, the GUI client was complete within 30 minutes.
Here is a screenshot, how it looks:
![]() |
| Just keep it simple |
![]() |
| It really works |
Source code
Source code can be found at github.Conclusion
This reminded me, that reinventing the wheel isn’t always a bad idea, there is always a chance to learn something new. And also how easy and fun it is to create a JavaFX application.Well done JavaFX and JavaFX Scene Builder teams!

