JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label wait. Show all posts
Showing posts with label wait. Show all posts

wait, notify and notifyAll method in java with example

7:55 AM
You might have noticed Object class has three final method called wait, notify and notifyAll. These methods are used for inter thread communication.  Java 5 has introduced executor framework which takes care of inter thread communication for you and internally uses wait, notify and notifyAll but you still require basic understanding of these methods and how inter thread communication works in java using wait , notify and notifyAll. What are wait , notify and notifyAll methods? wait , notify and notifyAll method are used to allow threads to communicate to each other via accessing common object or in other terms, Object can be considered a medium for inter thread communication via these methods. These methods need to be called from synchronized context,otherwise it will throw java.lang.IllegalMonitorStateException. Some background on synchronized block : Only one thread can enter at a time in synchronized block A thread required lock on the object to enter in synchronized bloc

.