I’m aware that sleep should be accessed in a static context. However I need more inputs so I can defend this to the management. Most of the legacy code that I’m handling now uses new Thread().sleep instead of Thread.sleep.
How bad is this?
for (int c = 0; c < 5; c++) {
new Thread().sleep(5000);
}
compared to this?
for (int c = 0; c < 5; c++) {
Thread.sleep(5000);
}
EDIT:
final long start = System.currentTimeMillis();
System.out.println("Total memory: " + Runtime.getRuntime().totalMemory());
System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
System.out.println("===========================");
for (int c = 0; c < 5; c++) {
new Thread().sleep(5000);
System.out.println("Used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
System.out.println("===========================");
}
System.out.println("Total memory: " + Runtime.getRuntime().totalMemory());
System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
System.out.println("===========================");
System.out.println("Time elapsed: " + (System.currentTimeMillis() - start) + " milliseconds");
Result (new Thread().sleep):
Total memory: 5177344
Free memory: 4990904
===========================
Used memory: 186440
===========================
Used memory: 205136
===========================
Used memory: 205136
===========================
Used memory: 205136
===========================
Used memory: 205136
===========================
Total memory: 5177344
Free memory: 4972208
===========================
Time elapsed: 24938 milliseconds
Result (Thread.sleep):
Total memory: 5177344
Free memory: 4990904
===========================
Used memory: 186440
===========================
Used memory: 186440
===========================
Used memory: 204848
===========================
Used memory: 204848
===========================
Used memory: 204848
===========================
Total memory: 5177344
Free memory: 4972496
===========================
Time elapsed: 24938 milliseconds
It is just illiterate programming, simple as that. Same applies to calling any static method that way, it’s not confined to Thread.sleep(). It imposes pointless space and time costs, but worse it betrays a major conceptual misunderstanding on the part of the programmer. I wouldn’t devote much energy to going back and fixing all the occurrences but I would certainly do something about re-educating the personnel concerned.