The Wait method in Java hold the thread to release the lock till the thread hold the object. The most important point is to be remember that wait method is used inside the synchronized code. The wait ( ) is defined inside the object class. When the wait method is called the following event to be occurred -
Understand with Example
The Tutorial understand a code that helps you to understand Java method Wait.In this Tutorial we have a class Wait Method declare a initialized string variable st .Inside the main method we instantiate a wait method class.
wait.start ( ) : This causes the thread to start or begin execution. The start ( ) method also allocate the system resources required for a thread. The current thread schedule to run and invoke the run method ( ).The print ln print the value "Hello".
notify All ( ) :This is used to wake up all the thread waiting for allocating the resources.
wait.valchange ( ) :The wait object call a Val change ( ) method, Finally the println print the value to be changed from Hello to Hello world.
Here is the code:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles