1

when should i use object.wait() method and when should i use Thread.sleep() in java?

In my understanding:

Wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution of current thread, generally.

i am asking in the context of usecases of both the methods.for e:g problems like producer consumer can be solved by wait(), notify().

  • That's more or less right, but they're more different than the same. I would say that the times to use Object.wait() are very rare. Most of the time, you can (and should) use a higher-level class, like CountDownLatch or BlockingQueue. – yshavit Mar 01 '19 at 04:51
  • And the times to use `Thread.sleep()` are even rarer. – user207421 Mar 01 '19 at 05:25
  • Please see my edited question - it's not duplicate – Sanidhya Kumar Mar 01 '19 at 07:18

0 Answers0