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?
Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization.
Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.
The major difference is to wait to release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution. This was just a clear and basic explanation, if you want more than that then continue reading.
Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With