버튼을 클릭하면 사용자가 일부 계산을 수행하는 작업 (호출 가능)을 만듭니다. 작업이 끝나면 반응 할 수 있기를 원합니다. Future.get()을 사용하면 응용 프로그램을 차단합니다. Callable이 결과를 반환 할 때 반응 할 수있는 방법이 있습니까?차단하지 않고 Java 결과를 기다리는 중
private static void startTask(int i){
try{
Future<Integer> future = executor.submit(callables.get(i-1));
ongoingTasks.put(i, future);
awaitResult(future, i);
}
catch(Exception e){
e.printStackTrace();
}
}
private static void awaitResult(Future<?> future, int taskNo) throws InterruptedException, ExecutionException{
System.out.println("result : " + future.get());
JButton b = buttons.get(taskNo);
b.setEnabled(false);
}
가능한 복제 [하는 Future.get에 메소드 호출() 블록. 정말 바람직한가요?] (http://stackoverflow.com/questions/31092067/method-call-to-future-get-blocks-is-that-really-desirable) – aUserHimself
계산은 두 번째 스레드에서 수행되어야합니다. 주 스레드 (또는 GUI 스레드)는 절대로 동결되지 않습니다. – Obenland
로직을 넣고 다른 스레드에서 실행할 수 있도록 콜백을 만들면 –