You really discuss design decisions, not performance decisions.
Your colleague's expression on how Thread.sleep is implemented is basically incorrect, as far as I can tell. On a smart JVM in a smart operating system, Thread.sleep () is implemented using its own O / S method to park the thread (or put it in a "timeout" state or whatever you want to call it on your OS), or to another, while the thread is sleeping, it consumes a zero processor. And in any case, TimerTask will use Thread.sleep (or similar - I don't just remember if it uses the park () method introduced in Java 5, but it doesn't really matter).
The JVM generally does not make secret decisions about ceiling processing. If you ask for another thread, you will get it; if you do not, you will not. For garbage collection, etc. Several home streams will be created, but as far as your code is concerned, you can assume that no covert clever creation of streams occurs.
So, back to your questions:
- sleeping in the main thread is completely safe; Obviously, while the main thread was sleeping, it will not do anything, but if you do not need it, then this is fine;
- regardless of whether you use Thread.sleep () directly in your code or use one of the timer utility methods, this is again a design decision based on whether you want to use a shared library that removes the resulting data from your code , or you prefer to have these details under your control; in terms of performance, this will have little chance if you implement things correctly;
- Do you have a static method or do you have an instance of Collector, this is not a fundamental decision - it is just a design decision that needs to be made on the basis of what seems more intuitive: you see Collector as a class of “general utility”, or as a “thing” with the state that you request to perform operations?
source share