2012-09-17 1 views
1

내 응용 프로그램에는 두 개의 jtextpanes가 있으며 몇 가지 작업이 포함 된 executorservice instacne이 있습니다. executorervice 인스턴스의 모든 작업이 모든 jtextpanes의 FocusListener focusGained 메소드를 실행하기 전에 완료되었는지 확인하고 싶습니다. 또한 focusLost 메서드가 호출 될 때 executorservice에 몇 가지 작업을 추가합니다.executorervice가 모든 작업을 완료 할 때까지 EDT 차단

코드

top.addFocusListener(new FocusListener() 
{ 

    public void focusLost(FocusEvent e) 
    { 

    if (ecaViewControl.isInitialized()) 
    { 
     stopRecording(); 
     executor.execute(new Runnable() 
     { 

     public void run() 
     { 
      ecaViewControl.save(top); 

     } 
     }); 

     executor.execute(new Runnable() 
     { 
     public void run() 
     { 
      ecaViewControl.closeDocument(); 

     } 
     }); 

    } 

    } 

    public void focusGained(FocusEvent e) 
    { 
    //Need to have completed all the task in executor service before EDT executes the code in here 

    }); 

} 
+0

그래서 모든 작업이 완료 될 때까지 UI를 차단 하시겠습니까? 그거 좋은 생각이야? – Tudor

답변

1

당신이 작업의 수를 완료 할 알고 있다면, 그래서 당신의 코드처럼 보일 수 있습니다 CountDownLatch

의 사용을 고려 : 코드의 다음

public void run() { 
    try { 
    // do something here 
    } finally { 
     latch.countDown() 
    } 
} 

작업 완료를 기다리고 있습니다. 래치를 기다리십시오.

latch.await(); 

또는 집행자없이 거주 할 경우 CompletionService을 대신 사용하십시오.

+0

나는 CountDownLatch를 시도했다. 이것은 코드입니다. – user1677675

관련 문제