Thursday, 11 May 2017

19. Inter thread communication via wait(), notify(),notifyAll()

1. Inter thread communication using wait(), notify(), notifyAll()
  1. Two threads can communicate with each other by using wait() , notify() and notifyAll().
  2. The thread which is expecting updation is responsible to call wait() then immediately the thread will entered into waiting state.
  3. The thread which is responsible to perform updation, after performing updation it is responsible to call notify() then waiting thread will get that notification and continue its execution with those updated items.
  4. Wait(), notify(),notifyAll() present in Object class, but not in thread class because thread can call these methods on any java object.
  5. To call Wait(), notify(),notifyAll() on any object , thread should be owner of that object. that is thread should has lock of that object. that is Wait(), notify(),notifyAll() should be in synchronized area.
  6. Hence we can call Wait(), notify(),notifyAll() mostly from synchronized area, otherwise we will get Runtime exception : IllegalMonitorStateException.
  7. If a thread calls wait() on any object it immediately releases lock of that particular object(not all locks) and entered into waiting state.
  8. If thread calls notify() on any object it releases the lock of that object but may not immediately after completing the remaining code.
  9. Except Wait(), notify(),notifyAll() there  is no other method where thread releases the lock.
  10. Below are the Syntax
    1. Public   final    void wait()   throws InterruptedExecption
    2. Public   final    void native   wait(long  milliSeconds)   throws InterruptedExecption
    3. Public   final    void wait(long ms, int ns)   throws InterruptedExecption
    4. Public   final   native   void notify()  
    5. Public   final   native   void notifyAll()  
  11. Every wait() throws Interruptedexception which is checked exception, whenever we are using wait() compulsory we should handle this exception either by try catch or throws keyword.
  12. Wait(long) will go to wait state until notify() called or up to specified time.
2. How many ways wait() thread will come back to runnable state from waiting state
  1. Waiting thread will go to runnable state in following cases(after getting lock of that object)
    1. After call notify(), notifyAll() calling on same object, and completion of notify() executed thread completes or releases lock of that object.
    2. If specified time completes.
    3. Any interruption on that thread.
    4. Any spurious wakeup


3. Can we write wait(), notify(), notifyAll() in non synchronized area
  1. Yes, we can write.
  2. Without synchronized area also we can use wait(), notify(), notifyAll() but thread must and should have lock of that object.
                 
4. Why wait(), notify(), notifyAll() are specified in Object class
  1. wait(), notify(), notifyAll() are related threads concept only.
  2. But these methods are not specified in Thread class.
  3. These methods are related to threads but we will not call on thread objects, we will call on any java object.
  4. Once if we call wait() on any java object then current thread must be the owner of that object(thread must acquire the lock of that object).
    s1.wait() → ‘s1’ is object of Student class, current thread must have lock of ‘s1’
  5. Which thread will execute the above line , that thread must have the lock of ‘s1’ object.
  6. Then the currently executing thread will go to waiting state and releases the lock.
  7. So thread can go to waiting state by releasing the lock of particular java object.
  8. So each and every class must contains wait(), because thread can acquire or release any java class object lock.
  9. Object class contains these methods, because each and every class will inherit these methods from Object class.
  10. Similarly notify() and notifyAll() also needs java object lock.
  11. Based on above points wait(), notify(), notifyAll() methods are in Object class.

5. Object class overloaded wait() → wait(long ms)  vs wait(long ms,  int ns)
  1. wait(long ms) → specifies the max waiting time in milliseconds.
  2. wait(long ms,  int ns) → specifies the max waiting time in nanoseconds.
  3. However, these waiting times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS.
  4. Also, the wait period can be terminated by interrupts.
  5. Wait period can be terminated by notify() or notifyAll() calls also.
  6. wait(long ms) is a native method so implementation is provided in native language like ‘c’.
  7. wait(long ms, int ns) is java method it will convert nanoseconds to equivalent milliseconds and call wait(long ms) internally.Add the total time (ms + ns)
  8. If we pass milliseconds value as -ve numbers or nanoseconds value is not in the range of 0-999999 than it throws IllegalArgumentException.
  9. Passing nanoseconds value is more than 0 then it will add to milli seconds.
  10. wait(0) is equal to wait() only.
6. Difference between notify() and notifyAll()
  1. We can use notify() to give notification for only one waiting thread.
  2. If multiple threads are waiting on same object, but one thread will be notified and remaining threads will wait for further notifications.
  3. Which thread will be notified , it dependents on JVM.
  4. So we need to use notifyAll() for notifying to all waiting threads on same object.
a.       Obj1.wait() -> 60 threads are in waiting state for Obj1 lock
b.      Obj2.wait() -> 40 threads are in waiting state for Obj2 lock
c.       Obj1.notifyAll() -> only obj1 60 threads are coming from wait state, but obj2  40 threads still in waiting state.(But one thread will execute at a time out of 60 threads )

No comments:

Post a Comment

3. Java Program to create Binary Tree