+1

What is different between Object.wait() and infinite while loop

Differenctiate Java programming Object.wait(); and while (true){ }

9/28/2019 3:29:49 PM

Ajay Gaikwad

2 Answers

New Answer

+2

The wait() method is used in the case of threads. If you want to perform some task by thread1 until another thread thread2 wait for the processor. For more details of Multi-threading https://javagoal.com/multithreading/ If the condition of the do-while loop will never false. Then it’s an infinite do-while loop. https://javagoal.com/do-while-loop/

+1

Object.wait() if must used method in multithreading. Difference between Object.wait() and infinite while loop is that if you call wait() method on any thread it will releases aquaried resources, that should be used by other threads. If you run infinite loop then the resources held by thread or process are not released which might be required for other process. Hence infinite loop consumes resources even if they are not in use. But in Object.wait() case is totally opposite and it releases resources. While waiting and wakes up it receive signal from notify or notifyAll methods.