[go: up one dir, main page]

0% found this document useful (0 votes)
775 views2 pages

Inter-Thread Communication in Java: 1) Wait Method

Inter-thread communication in Java allows synchronized threads to communicate with each other using wait(), notify(), and notifyAll() methods of the Object class. Wait() causes the current thread to release the lock and wait until another thread calls notify() or notifyAll(). Notify() wakes up one waiting thread while notifyAll() wakes up all waiting threads. An example demonstrates a customer object with withdraw and deposit threads that use wait() and notify() to coordinate access to a shared balance.

Uploaded by

RASHMI DABRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
775 views2 pages

Inter-Thread Communication in Java: 1) Wait Method

Inter-thread communication in Java allows synchronized threads to communicate with each other using wait(), notify(), and notifyAll() methods of the Object class. Wait() causes the current thread to release the lock and wait until another thread calls notify() or notifyAll(). Notify() wakes up one waiting thread while notifyAll() wakes up all waiting threads. An example demonstrates a customer object with withdraw and deposit threads that use wait() and notify() to coordinate access to a shared balance.

Uploaded by

RASHMI DABRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Inter-thread communication in Java

Inter-thread communication or Co-operation is all about allowing synchronized


threads to communicate with each other.

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused


running in its critical section and another thread is allowed to enter (or lock) in the same
critical section to be executed.It is implemented by following methods of Object class:

o wait()
o notify()
o notifyAll()

1) wait() method

Causes current thread to release the lock and wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a specified amount of
time has elapsed.

The current thread must own this object's monitor, so it must be called from the
synchronized method only otherwise it will throw exception.

2) notify() method

Wakes up a single thread that is waiting on this object's monitor. If any threads are
waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and
occurs at the discretion of the implementation. Syntax:

public final void notify()

3) notifyAll() method

Wakes up all threads that are waiting on this object's monitor. Syntax:

public final void notifyAll()


Example of inter thread communication in java
Let's see the simple example of inter thread communication.

1. class Customer{  
2. int amount=10000;  
3.   
4. synchronized void withdraw(int amount){  
5. System.out.println("going to withdraw...");  
6.   
7. if(this.amount<amount){  
8. System.out.println("Less balance; waiting for deposit...");  
9. try{wait();}catch(Exception e){}  
10. }  
11. this.amount-=amount;  
12. System.out.println("withdraw completed...");  
13. }  
14.   
15. synchronized void deposit(int amount){  
16. System.out.println("going to deposit...");  
17. this.amount+=amount;  
18. System.out.println("deposit completed... ");  
19. notify();  
20. }  
21. }  
22.   
23. class Test{  
24. public static void main(String args[]){  
25. final Customer c=new Customer();  
26. new Thread(){  
27. public void run(){c.withdraw(15000);}  
28. }.start();  
29. new Thread(){  
30. public void run(){c.deposit(10000);}  
31. }.start();  
32.   
33. }}  
Output: going to withdraw...
Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed

You might also like