The concept of wait and sleep in java threading i couldn't get easily . I read at several tech blogs and docs that wait should be called by object while sleep is called by thread . Both methods are used to pause a running thread momentarily . However, i couldn't understand for a long , what does that mean by calling a object ? If wait is to be called by object then why Thread.currentthread.wait () is visible ? because an object can be of a thread also .
The thing is a Thread when wants to wait . it should hold an object & use that object to wait or either it can directly call Thread.sleep(). In Former case object should be either notify or be provided few millisecs to wait . sleep() can be preferred to call when thread has to wait for small fraction of time .
Both wait() and sleep() cause the current
thread to pause execution, not the thread on which you you call them.
There is no way to cause a different thread to pause.
If wait is being called on a thread then it would give IllegalMonitorStateException .
No comments:
Post a Comment