2010-06-06 6 views
1

IOBound 작업을 실행하는 SwingWorker 스레드가 있는데,이 스레드는 실행되는 동안 인터페이스를 완전히 잠그고 있습니다. 카운터 루프에 대한 일반 작업 부하를 교체하면 동일한 결과가 발생합니다. SwingWorker의는 다음과 같이 기본적으로 같습니다SwingWorker 스레드가 실행되는 동안 내 GUI가 응답하지 않는 이유는 무엇입니까?

public class BackupWorker extends SwingWorker<String, String> { 

private static String uname = null; 
private static String pass = null; 
private static String filename = null; 
static String status = null; 

BackupWorker (String uname, String pass, String filename) { 
    this.uname = uname; 
    this.pass = pass; 
    this.filename = filename; 
} 

@Override 
protected String doInBackground() throws Exception { 
      BackupObject bak = newBackupObject(uname,pass,filename); 
    return "Done!"; 
} 

}

JFrame의를 확장하는 클래스에서의 삶을 그것을 개막 코드 :

:

public void actionPerformed(ActionEvent event) { 
    String cmd = event.getActionCommand(); 

    if (BACKUP.equals(cmd)) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

       final StatusFrame statusFrame = new StatusFrame(); 
       statusFrame.setVisible(true); 

       SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         statusFrame.beginBackup(uname,pass,filename); 
        } 
       }); 
      } 
     }); 
    } 
} 

여기 StatusFrame의 흥미로운 부분이다

public void beginBackup(final String uname, final String pass, final String filename) { 
    worker = new BackupWorker(uname, pass, filename); 
    worker.execute(); 

    try { 
     System.out.println(worker.get()); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 
} 

}

내가 볼 수있는 한, "장기 실행"되는 모든 것은 작업자와 EDT의 GUI에 닿는 모든 것이 처리됩니다. 나는 어딘가에 물건을 얽힌 적이 있습니까, 아니면 너무 많은 SwingWorker를 기대합니까?

답변

6

beginBackup 메서드에서 SwingWorker.get() 전화로 인한 문제라고 생각합니다. 완료 계산 에 대한

대기 필요한 경우, 다음의 결과를 검색하십시오 docs for this method를 살펴 보자.

이것은 차단 호출이므로 GUI가 응답하지 않게됩니다.

(또한,이 invokeLater 호출 내에서 invokeLater은? 이미 동부 서머 타임에 실행하고하고있는 이유는 어떤 특별한 이유가있다.)

+0

여기에서 저를 버렸던 것은'get()'에'while (! worker.isDone()) '을 앞에두고 동일한 응답이없는 것을 보았습니다. 'StatusFrame'이 끝나기 전에 GUI가 잠겨있는 것을보고 있었는데, 그때 못생긴'Runnable' 중첩을 시도했습니다. 그 문제는 지금 당장 의문입니다. – Starchy

0

에 대한 Tasks That Have Interim Results에 스윙 튜토리얼에서 섹션을 읽어 작업 예제. SwingWorker 클래스에서 재정의 된 process (...) 메서드 내에서 get (...) 메서드가 호출된다는 것을 알 수 있습니다.

관련 문제