2013-03-28 2 views
0

임 : IllegalMonitorStateException을 얻었으며 더 읽을 수 없다고 느낍니다. 그래서 누군가 임 무언가를 잘못 알고 있는지 묻습니다. 임, 게이트 클래스의 스레드 중 공유 리소스) 대기 후 붙어있는 프로그램의 끝 에서이 오류가 점점. 쓰레드를 끝내라고 말하면 나는 main 메소드에서 그것들을 인터럽트한다.스레드를 인터럽트 할 때 IllegalMonitorStateException이 발생했습니다.

카운터 클래스에는 다른 중요한 방법이 있습니다.

  private int currentValue; /* to keep score of people passing */ 

      /* critical section */ 
    public synchronized boolean isFull() { 
     if(busy) { /* lock */ 
      try { 
       wait(); 
      } catch (InterruptedException e) { 
       System.out.println("isFull was interrupted waiting"); 
      } 
     } 
     busy=true; 
        /* is not full notify threads waiting to resume work*/ 
     if(currentValue < maxValue) { 
      notify(); 
      busy=false; /* unlock */ 
      return false; 
     } 
        /* do not notify threads */ 
     return true; 
    } 

      /* critical section */ 
    public synchronized void addPersons() { 
     if(busy) { 
      try { 
       wait(); 
      } catch (InterruptedException e) { 
       System.out.println("addPersons was interrupted waiting"); 
      } 
     } 
     busy=true; 
     currentValue += nr_p; 
     notify(); 
     busy=false; 
    } 

      public synchronized int getValue() { 
     return currentValue; 
    } 

클래스 게이트의 실행 방법 (또한 스레드를 가질 수) :

  public void run() { 
     while(!blocked) { 
      int waitTime = (r.nextInt(5) + 1) * 1000; /* random seconds */ 
      if(counter.isFull(gatenr)) 
       try { 
         t.wait(); /* here is where the IllegalMonitorStateException occurs. */ 
       } catch (InterruptedException e) { 
        System.out.println("Shutting gate off from waiting"); 
       } 
      if(t.isInterrupted()) { /* check if thread is interrupted */ 
       System.out.println("breaking"); 
       break; 
      } 
      counter.addPersons(1); 
      nrOfPassengers++; 
      try { 
       Thread.sleep(waitTime); 
      } catch (InterruptedException e) { 
       System.out.println("Shutting gate off from sleep"); 
      } 
     } 
    } 

     /* used by main to interrupt threads from wait statement and close gate */ 
    public synchronized void close() { 
     t.interrupt(); 
     blocked = true; 
    } 

주요 방법이 라인이 있습니다

  while(counter.getValue() < MAX) { 
       Thread.sleep(3000); 
      } 
      System.out.println("Announcement: Ship is full!"); 


      /* wait for child threads to finish */ 
      for(Gate g: gates) { 
       g.close(); /* interrupts thread in gate object */ 
       System.out.println(g.getNoOfPassangers() + " passed through gate nr " + g.getId()); 
      } 
      } 

이 운전은 나를 미치게을, I 모니터 오류에 대해 모두 읽었습니다.

+0

From docs : 스레드가 객체 모니터에서 대기하려고 시도했거나 지정된 모니터를 소유하지 않고 객체 모니터에서 대기중인 다른 스레드에 알리려고했음을 나타 내기 위해 발생합니다. – Jayan

+0

잠금 용 멤버 변수를 만듭니다. 그리고 전화 만 기다려. 그것은 약간 도움이됩니다. http://stackoverflow.com/questions/2536692/a-simple-scenario-using-wait-and-notify-in-java에서 일부를 참조하십시오. – Jayan

답변

1

t.wait();synchronized(t) 블록이어야합니다.

그러나 코드에 t.notifyAll()이 표시되지 않아 영원히 기다릴 수없는 것처럼 보입니다. 또한 표준 관용법은 위조 된 웨이크 업을 처리하기 위해 루프를 기다리는 것입니다.

관련 문제