0
What is the mean of wait ()
3 odpowiedzi
+ 1
public class P
{
int amt=10000;
synchronized void withdraw(int amt)
{System.out.println("going to withdraw...");
if(this.amt<amt)
{
System.out.println("insufficient balance waiting for deposit");
try{
wait(); //releases lock from withdraw()
} catch(Exception e) {}
}this.amt=amt;
System.out.println("withdraw completed");
}
synchronized void deposit(int amt)
{
System.out.println("depositing");
this.amt=amt;
System.out.println("deposit completed ");
notify();// will give lock again to withdraw()
}
public static void main(String[] args) {
final P p=new P();
new Thread()
{public void run()
{p.withdraw(15000);}}.start();
new Thread()
{public void run()
{p.deposit(15000);}}.start(); }
}
//hoping that you have basic knowledge if any problem with this code u may ask me doubts I will try to clear
0
it is a pre defined method of Object class,
it is used to release lock from a particular thread and send it in the waiting state till another method notify() is used to provide lock to that particular thread.
also we can release a lock/monitor for a particular amount of time by using wait(long time_out).
0
Could you please explain with example.