Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 992
Omu Avatar asked Jan 16 '10 11:01

Omu


People also ask

What is difference between sleep () and wait ()?

Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization.

What does sleep () do in thread Java?

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.

What is difference between thread sleep and thread wait?

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.

Why thread sleep is not recommended Java?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.


1 Answers

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.

like image 62
Sean Owen Avatar answered Oct 11 '22 11:10

Sean Owen