What is the difference between wait sync




















Sleep method belongs to Thread class. Wait method releases lock during Synchronization. Sleep method does not release the lock on object during Synchronization.

Wait should be called only from Synchronized context. There is no need to call sleep from Synchronized context. Wait is not a static method. Sleep is a static method. Both are Native Methods. After x seconds pass, go to the next step immediately. Sync wait for page showing up completely upto object synchronization timeout. If synchronization timeout reaches but the page doesn't show up completely, then fail.

View a Printable Version Subscribe to this thread. Linear Mode. Threaded Mode. So, yes, it's thread-safe. The two concepts are combined, not mutually-exclusive. When you use wait you need to own the monitor on that object. So you need to have synchronized.. So after just being embarrassed in an interview question on this I decided to look it up and understand it again for 1 billionth time. No doubt about that. When wait and notify or notifyAll come in is where you are trying to write more efficient code.

If they were allowed without synchronization, there were occur lots of other problems such as race condition, data corruption and even program crash. Please note that a program does not get any kind of surety from low level OS thread scheduling, so it depends on language runtime to manage its thread. Thanks for the answer Lokesh.. I have 1 more question!!! Pardon for being lazy here, just came from office. It is so because, Thread 2 will not have the idea of Thread 1 state, where t1 is in wait state or it is notified.

I will say partly because real problem is to find out which all threads are in application and then which one are waiting on current monitor. Tricky question.. In my view, no major impact on application just because one of several hundreds thread is blocked somewhere. Rest of application will run fine. This is very much clear and easy. No, program will not hang. According to theory, it should not allow to terminate and you might need to do it forcefully.

Good points you are covering here, it will be more helpful if you take up the relatively new topics like concurrent package, annotations, generics, nio etc. A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python. About Me. Contact Us.



0コメント

  • 1000 / 1000