Thursday 11 May 2017

20. Missed signals and Spurious Wakeups

1. Is that give any effect if we call notify() or notifyAll() before calling of wait()
  1. No, before wait() if we call notify() or notifyAll() call is wasted.
  2. If we call notify() or notifyAll() it will check any thread is in waiting state respective to corresponding object lock.
  3. If no thread is available in waiting state on specified lock, then call is wasted.
  4. After the notify calls if we call wait() then the thread will go to waiting state by releasing the lock.
  5. It will be in waiting state up to again call notify() or notifyAll() or any below case.
    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
2. What is missed signal in thread communication.
  1. From above concept we understand if we call notify methods before calling wait() then there is no effect on notify methods.
  2. So calling notify methods are giving signals to waiting threads.
  3. Sometimes there may be a chance of missing signals like below case.
    1. In my application 2 threads are there for communication.
    2. 1st thread need to go wait state and 2nd thread give the signal to 1st thread by calling notify methods.
    3. But my 2nd thread executed first before my 1st thread, then the thread is calling notify methods before wait() by mistake.
    4. Now 1st thread is executing wait()
  4. In this case 1st thread will be in waiting state forever.
  5. So this type of cases are called as missing signals.




3. How to avoid missed signal in thread communication.
  1. We need to maintain some logic like if signal is not available then only my thread need to go wait state.
  2. Else my thread need to skip wait state.
          
  3. So we need to maintain flag or boolean variable to is signal is fired or not.


4. What is  spurious wakeups or spin lock in thread communication.
  1. For inexplicable reasons it is possible for threads to wake up even if notify() and notifyAll() has not been called.
  2. This is known as spurious wakeups. Wakeups without any reason.
  3. If a spurious wakeup occurs in the Display class's wish() method the waiting thread may continue processing without having received a proper signal to do so! This could cause serious problems in your application.
  4. To guard against spurious wakeups the signal member variable is checked inside a while loop instead of inside an if-statement.
  5. Such a while loop is also called a spin lock. The thread awakened spins around until the condition in the spin lock (while loop) becomes false.
5. Why spurious wakeups are occurred in java.
  1. It is a very rare scenario to occur spurious wakeups in java application.
  2. In java 1.5 they introduced one daemon thread which runs on background of our application its priority of the thread is very less, so it will get less number of chances to execute.
  3. The daemon thread is ‘malicious alarm clock’ introduced in jdk 1.5
  4. The thread will get random object and calls notifyAll() , so some times only it will give spurious wakeups.
6. How locks will be released from thread in reentrance synchronization.
Locks in synchronization
  1. If thread entered into synchronized area, then thread will acquire object lock.
  2. After completion of the synchronized area only thread will releases the lock.
  3. In middle of synchronized area if we write calling wait() on thread acquired object.
  4. Thread will releases the lock and go to waiting state and waiting for notification on that object.
  5. Some other thread will acquire the lock and executes its synchronized areas of that object.
Locks in reentrance synchronization
  1. If thread entered into same object lock related synchronized area again and again then thread will hold same object lock multiple times.
  2. In middle of synchronized area if we write calling wait() on thread acquired object.
  3. Thread will releases the all the locks of same object and go to waiting state and waiting for notification on that object.

No comments:

Post a Comment

3. Java Program to create Binary Tree