H
Harald Kirsch
Consider the following code taken from
one and the same class declaring a field
and two methods.
// a field of the class
private volatile boolean done;
void method1() {
done = false;
synchronized(someObject) {
while(!done) someObject.wait();
}
}
void method2()
synchronized(someObject) {
done = true;
someObject.notifyAll();
}
}
I have two questions:
1) is the `volatile' keyword necessary to make sure
that method1() never finds an outdated value when
returning from wait().
2) Does the single call to notifyAll() guarantee that
method1() will eventually return from wait(), even if
other threads are either waiting on the same monitor or
are blocked by that monitor?
Thanks,
Harald Kirsch.
one and the same class declaring a field
and two methods.
// a field of the class
private volatile boolean done;
void method1() {
done = false;
synchronized(someObject) {
while(!done) someObject.wait();
}
}
void method2()
synchronized(someObject) {
done = true;
someObject.notifyAll();
}
}
I have two questions:
1) is the `volatile' keyword necessary to make sure
that method1() never finds an outdated value when
returning from wait().
2) Does the single call to notifyAll() guarantee that
method1() will eventually return from wait(), even if
other threads are either waiting on the same monitor or
are blocked by that monitor?
Thanks,
Harald Kirsch.