Java: Thread.currentThread().sleep(x) vs. Thread.sleep(x)

JavaEclipseMultithreadingSleepWarnings

Java Problem Overview


I have this in my code

Thread.currentThread().sleep(x);

Eclipse tells me to use the static

Thread.sleep(x); 

instead, why? What's the difference, is there some difference in functionality at all between these 2 methods?

Java Solutions


Solution 1 - Java

There is only one method, not two, and it is static. While you can call a static method via an instance reference, it's not good style. It indicates the programmer thinks he or she is calling an instance method. A confused programmer might be thinking he or she can cause another thread (not the current one) to sleep this way, when that's not what it does.

Both your lines of code do the same thing but the second is better style.

Solution 2 - Java

In Java, sleep is a static method. Both your examples do exactly the same thing, but the former version is confusing because it looks like it is calling a method on a specific object but it's not doing that at all. In your example it won't matter much, but it is more dangerous if you have the following:

someOtherThread.sleep(x);

This time it looks like you are telling some other thread to sleep, but in fact you are putting the current thread to sleep. The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object.

Solution 3 - Java

The two method calls are identical in behavior because they invoke the same method but using the class name (Thread in this case) rather than instance for accessing static fields and methods makes this static-ness clear. That is why this warning is produced.

But considering that static fields and methods are shown in a particular way in most IDEs (for example in italic font in Eclipse and IntelliJ IDEA), is this warning still necessary? Maybe not as much necessary as it was in the early days of Java that simple editors were in use.

Solution 4 - Java

Thread.currentThread().sleep(x); or the way Eclipse says so Thread.sleep(x); static context is required if it is in the need, so we hold on for slight delay with this sleep.

Static paradigm set by one object, only affects that particular object heap print life cycle, again considering the overall Object Life Cycle static is not that bothersome, if required it can be used to ease the coding, but to be done carefully as static foot-print is referred by Class (for example:- Class.forName(pkg.className)) as like by name and not by any object which is run-time single print copy of Class in HEAP memory.

Again usage of object also has pros & cons by Weak, Phantom, Strong kind of references....,

Code is Convoluted by Nature. It is just the way how we do in order to make it work & functional.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOmuView Question on Stackoverflow
Solution 1 - JavaSean OwenView Answer on Stackoverflow
Solution 2 - JavaMark ByersView Answer on Stackoverflow
Solution 3 - JavaAmir MoghimiView Answer on Stackoverflow
Solution 4 - JavaDev Anand SadasivamView Answer on Stackoverflow