N
Nafai
Let's have an example (just a generic program with threads and a monitor)
class MyMonitor {
....
public synchronized void m()
{
...
while(...) wait(); // (A)
...
notifyAll();
}
};
class MyThread implements Runnable {
....
MyMonitor m;
MyThread(MyMonitor m) { this.m=m;}
public void run()
{
while(true){
...
sleep(...);
// A thread can get monitor here even if there
// are threads waiting (but already notified) in (A)
m();
...
}
}
}
class MainProgram {
public static void main(String[] argv)
{
MyMonitor m = new MyMonitor();
MyThread t1 = new MyThread(m);
MyThread t2 = new MyThread(m);
t1.start();
t2.start();
}
How can I avoid that a thread (which is about to get into the monitor)
steals the turn of the monitor of a thread which has been waiting but
has been notified?
Thanks.
class MyMonitor {
....
public synchronized void m()
{
...
while(...) wait(); // (A)
...
notifyAll();
}
};
class MyThread implements Runnable {
....
MyMonitor m;
MyThread(MyMonitor m) { this.m=m;}
public void run()
{
while(true){
...
sleep(...);
// A thread can get monitor here even if there
// are threads waiting (but already notified) in (A)
m();
...
}
}
}
class MainProgram {
public static void main(String[] argv)
{
MyMonitor m = new MyMonitor();
MyThread t1 = new MyThread(m);
MyThread t2 = new MyThread(m);
t1.start();
t2.start();
}
How can I avoid that a thread (which is about to get into the monitor)
steals the turn of the monitor of a thread which has been waiting but
has been notified?
Thanks.