код 1:java бесконечный вопрос ожидания?
class BCWCExamples {
public Object lock;
boolean someCondition;
public void NoChecking() throws InterruptedException {
synchronized(lock) {
//Defect due to not checking a wait condition at all
lock.wait();
}
}
Код 2:
public void IfCheck() throws InterruptedException {
synchronized(lock) {
// Defect due to not checking the wait condition with a loop.
// If the wait is woken up by a spurious wakeup, we may continue
// without someCondition becoming true.
if(!someCondition) {
lock.wait();
}
}
}
Код 3:
public void OutsideLockLoop() throws InterruptedException {
// Defect. It is possible for someCondition to become true after
// the check but before acquiring the lock. This would cause this thread
// to wait unnecessarily, potentially for quite a long time.
while(!someCondition) {
synchronized(lock) {
lock.wait();
}
}
}
код 4:
public void Correct() throws InterruptedException {
// Correct checking of the wait condition. The condition is checked
// before waiting inside the locked region, and is rechecked after wait
// returns.
synchronized(lock) {
while(!someCondition) {
lock.wait();
}
}
}
}
Примечание: там сообщает от какой-то другой место
нет условия для ожидания в коде 1 так бесконечное ожидание, но почему бесконечное ожидание происходит в другом 3 коде (2,3,4)
любезно проверить комментарии в коде для лучшего понимания любезно помогите мне, я новичок в java wait and notify
Является ли это entireity соответствующего кода? Значение someCondition никогда не установлено в true –