The Wait method in Java hold the thread to release the lock till the thread hold the object.
JAVA Method Wait
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 -
- The other thread invoke the notify method for the this object.
- The other thread invoke the notify all ( ) method for the same object.
- The thread try to interrupt.
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:
public class WaitMethod extends Thread{
|