This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Deepak Lal wrote:Hi i have the below clarification.
1> Difference's between sleep and yield state in Java ?
Do you mean sleep() and yield() methods of Thread class? If so,
Calling sleep() from any thread halts the execution of the given thread for a specific period of time without losing the monitor of the thread. On the other hand invoking Thread.yield() causes the currently executing thread object to temporarily pause and allow other/this thread to execute. And both, sleep() and yield() don't release the acquired lock!
I couldn't understand your next question!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sleep() means it will stop the execution of thread for a particular time.
yield() will change the state of thread form runing state to runnable state,so that the thread of same priority will get the chance to run.
Sleep throws Interrupted Exception i.e we have put call to sleep inside a try/catch block otherwise compiler error wil be throws.
Yield is a static method..
Both the sleep and yielddoesnot guarantee that they will do what they expect to do.
As mentioned by above that both donot even release the lock on the acquire lock on the object
Shanky Sohar wrote:
We can invoke static method from a instance also.there will not be any problem..and also we can also invoke yield and sleep on the instance of a same object.
one will occupy a look on the class object other will occupy a look on a Class
I don't mean this. You can do whatever you've said! But, let's take threadA and threadB as two thread instances, and in you run() method of threadA, you invoked the sleep() method on threadB. What would happen?
static methods don't have any idea about instances of that class. So, the static methods are invoked on class basics, therefore, sleep() method will invoked on whatever the thread running at the time.
Hope you understood this! Is this para makes sense? Thanks!
Could you Please cite an example for this so that it can we used for later reference and be explained properly with comments...I have understood the basic differences but if you could oblige by citing an example it would be great enough.