What is the mean of wait () | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the mean of wait ()

3rd Oct 2016, 4:31 PM
sabreen anjum
sabreen anjum - avatar
3 Answers
+ 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
3rd Oct 2016, 5:59 PM
Ankur Sharma
Ankur Sharma - avatar
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).
3rd Oct 2016, 5:04 PM
Ankur Sharma
Ankur Sharma - avatar
0
Could you please explain with example.
3rd Oct 2016, 5:15 PM
sabreen anjum
sabreen anjum - avatar