http://massivetechinterview.blogspot.in/2015/09/java-concurrency.html
Missed Signals
The methods notify() and notifyAll() do not save the method calls to them in case no threads are waiting when they are called. The notify signal is then just lost. Therefore, if a thread calls notify() before the thread to signal has called wait(), the signal will be missed by the waiting thread. This may or may not be a problem, but in some cases this may result in the waiting thread waiting forever, never waking up, because the signal to wake up was missed.
To avoid losing signals they should be stored inside the signal class. In the MyWaitNotify example the notify signal should be stored in a member variable inside the MyWaitNotify instance. Here is a modified version of MyWaitNotify that does this:
public class MyWaitNotify2{
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
public void doWait(){
synchronized(myMonitorObject){
if(!wasSignalled){
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
//clear signal and continue running.
wasSignalled = false;
}
}
public void doNotify(){
synchronized(myMonitorObject){
wasSignalled = true;
myMonitorObject.notify();
}
}
}
Spurious Wakeups
For inexplicable reasons it is possible for threads to wake up even if notify() and notifyAll() has not been called. This is known as spurious wakeups. Wakeups without any reason.
To guard against spurious wakeups the signal member variable is checked inside a while loop instead of inside an if-statement. 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.
public class MyWaitNotify3{
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
public void doWait(){
synchronized(myMonitorObject){
while(!wasSignalled){
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
//clear signal and continue running.
wasSignalled = false;
}
}
public void doNotify(){
synchronized(myMonitorObject){
wasSignalled = true;
myMonitorObject.notify();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
http://quicktechie.com/cs/book-java-multithreading-questions/57-java-multithreading-questions/520-chapter-5-java-multi-threading-questions
Q43. What do you mean by missed signals?
Ans: It is possible that a thread can miss a signal. Let’s assume there are two threads ThreadA and ThreadB. ThreadA is going to enter in wait() state and ThreadB is going to notify(). However, ThreadB notify() ThreadA before ThreadA enter in wait() block. In this case notify() signal from ThreadB is missed by ThreadA. Hence, ThreadA remain in wait state forever, which could be a big problem. Missed signal problem can be avoided using proper code block around shared object.
Q44. What is a spurious wakeup?
Ans: It is possible because of any surprise reason like hardware issue, thread gets signal. Lets assume again we have two threads ThreadA and ThreadB , ThreadA is waiting on notification from ThreadB. However, ThreadA gets notification that ThreadB has done with its processing. This is a false signal and known as a spurious wakeup of ThreadA.
To avoid spurious wakeup issue, we should check wait state of shared object in while loop. So in case of false signal it has to check condition on shared object again.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
https://stackoverflow.com/questions/17032207/java-can-we-miss-signals-using-wait-and-notify
A missed signal can occur when you have two threads where one calls notify() before the other calls wait(). The only way to avoid this happening is to have some kind of barrier that makes the second thread wait until the first thread has called notify() - one mechanism that can be used to create this barrier is a semaphore :)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
https://fizalihsan.github.io/technology/java-concurrency.html
Re-entrant locks/Recursive locking
Once a thread acquires the lock of an object, it can call other synchronized methods on that object without having to wait to acquire the lock again. Otherwise the method call would get into deadlock.
Condition
instead of spin wait (sleep until a flag is true) which is inefficient, use Condition. Even better is to use coordination classes like CountDownLatch or CyclicBarrier which is concise and better than Condition.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
http://www.careerride.com/Java-semaphore-monitors.aspx
Semaphore:
A semaphore is a construct in multithreading. It is a construct that can be used to send signals between synchronized threads in order to avoid missing signals. It can also be used to guard critical operations such as dead locks.
Monitor:
Thread monitor supports the threads which are synchronized and mutual exclusion and co-operation. Multiple threads will work independently on shared data without interfering each other with the help of object locks concept. Cooperation between threads is performed with the usage of wait() and notify() methods of Object class. Cooperation is enabling threads work together for a common goal.
Synchronization allows the threads to execute one after another without interrupting the thread that is running. Monitor contains special memory location which allows storing a thread’s data and handles one at a time.
Missed Signals
The methods notify() and notifyAll() do not save the method calls to them in case no threads are waiting when they are called. The notify signal is then just lost. Therefore, if a thread calls notify() before the thread to signal has called wait(), the signal will be missed by the waiting thread. This may or may not be a problem, but in some cases this may result in the waiting thread waiting forever, never waking up, because the signal to wake up was missed.
To avoid losing signals they should be stored inside the signal class. In the MyWaitNotify example the notify signal should be stored in a member variable inside the MyWaitNotify instance. Here is a modified version of MyWaitNotify that does this:
public class MyWaitNotify2{
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
public void doWait(){
synchronized(myMonitorObject){
if(!wasSignalled){
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
//clear signal and continue running.
wasSignalled = false;
}
}
public void doNotify(){
synchronized(myMonitorObject){
wasSignalled = true;
myMonitorObject.notify();
}
}
}
Spurious Wakeups
For inexplicable reasons it is possible for threads to wake up even if notify() and notifyAll() has not been called. This is known as spurious wakeups. Wakeups without any reason.
To guard against spurious wakeups the signal member variable is checked inside a while loop instead of inside an if-statement. 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.
public class MyWaitNotify3{
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
public void doWait(){
synchronized(myMonitorObject){
while(!wasSignalled){
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
//clear signal and continue running.
wasSignalled = false;
}
}
public void doNotify(){
synchronized(myMonitorObject){
wasSignalled = true;
myMonitorObject.notify();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
http://quicktechie.com/cs/book-java-multithreading-questions/57-java-multithreading-questions/520-chapter-5-java-multi-threading-questions
Q43. What do you mean by missed signals?
Ans: It is possible that a thread can miss a signal. Let’s assume there are two threads ThreadA and ThreadB. ThreadA is going to enter in wait() state and ThreadB is going to notify(). However, ThreadB notify() ThreadA before ThreadA enter in wait() block. In this case notify() signal from ThreadB is missed by ThreadA. Hence, ThreadA remain in wait state forever, which could be a big problem. Missed signal problem can be avoided using proper code block around shared object.
Q44. What is a spurious wakeup?
Ans: It is possible because of any surprise reason like hardware issue, thread gets signal. Lets assume again we have two threads ThreadA and ThreadB , ThreadA is waiting on notification from ThreadB. However, ThreadA gets notification that ThreadB has done with its processing. This is a false signal and known as a spurious wakeup of ThreadA.
To avoid spurious wakeup issue, we should check wait state of shared object in while loop. So in case of false signal it has to check condition on shared object again.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
https://stackoverflow.com/questions/17032207/java-can-we-miss-signals-using-wait-and-notify
A missed signal can occur when you have two threads where one calls notify() before the other calls wait(). The only way to avoid this happening is to have some kind of barrier that makes the second thread wait until the first thread has called notify() - one mechanism that can be used to create this barrier is a semaphore :)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
https://fizalihsan.github.io/technology/java-concurrency.html
Re-entrant locks/Recursive locking
Once a thread acquires the lock of an object, it can call other synchronized methods on that object without having to wait to acquire the lock again. Otherwise the method call would get into deadlock.
Condition
instead of spin wait (sleep until a flag is true) which is inefficient, use Condition. Even better is to use coordination classes like CountDownLatch or CyclicBarrier which is concise and better than Condition.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
http://www.careerride.com/Java-semaphore-monitors.aspx
Semaphore:
A semaphore is a construct in multithreading. It is a construct that can be used to send signals between synchronized threads in order to avoid missing signals. It can also be used to guard critical operations such as dead locks.
Monitor:
Thread monitor supports the threads which are synchronized and mutual exclusion and co-operation. Multiple threads will work independently on shared data without interfering each other with the help of object locks concept. Cooperation between threads is performed with the usage of wait() and notify() methods of Object class. Cooperation is enabling threads work together for a common goal.
Synchronization allows the threads to execute one after another without interrupting the thread that is running. Monitor contains special memory location which allows storing a thread’s data and handles one at a time.
No comments:
Post a Comment