2016-12-13 1 views
0

코드 1 :자바 무한 대기 문제?

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)
친절 이해에 대한 코드의 주석을 확인에서 발생 친절하게 나를 도와주세요 자바에서 기다리며 알리십시오

+0

이는 관련 코드의 entireity인가? 값 someCondition이 절대로 true로 설정되지 않습니다. –

답변