2011. 8. 27. 16:15
futureTask JAVA이야기2011. 8. 27. 16:15
thread를 시작해 두고, (start()메소드)
get()메소드를 호출할 때, 제품 정보를 모두 가져왔다면 즉시 ProductInfo를 알려줄 것이고, 아직 데이터를 가져오는 중이라면 완료할 때까지 대기하고 결과를 알려준다.
Callable 인터페이스는 Runnable과 유사하지만, 다른점은 스레드의 상태를 알 수 있다는 점이 다르다.
private final FutureTask<ProductInfo> future =
get()메소드를 호출할 때, 제품 정보를 모두 가져왔다면 즉시 ProductInfo를 알려줄 것이고, 아직 데이터를 가져오는 중이라면 완료할 때까지 대기하고 결과를 알려준다.
Callable 인터페이스는 Runnable과 유사하지만, 다른점은 스레드의 상태를 알 수 있다는 점이 다르다.
private final FutureTask<ProductInfo> future =
new FutureTask<ProductInfo>(new Callable<ProductInfo>() {
public ProductInfo call() throws Exception {
return loadProductInfo();
};
});
private final Thread thread = new Thread(future);
public void start(){ thread.start();};
protected ProductInfo loadProductInfo() {
// TODO Auto-generated method stub
//시간이 많이 걸리는 작업
return null;
}
public ProductInfo get() throws InterruptedException, ExecutionException{
try{
return future.get();
}catch (ExecutionException e) {
// TODO: handle exception
Throwable cause = e.getCause();
}
return null;
}
private class ProductInfo{
}
'JAVA이야기' 카테고리의 다른 글
작업별로 스레드를 만드는 것은 자원관리 측면에서 허점이 있다. (0) | 2011.08.28 |
---|---|
Barrier (0) | 2011.08.28 |
CountDownLatch (0) | 2011.08.27 |
반복문을 실행하는 코드를 동기화시키는 방법은 비효율적이다. (0) | 2011.08.27 |
AppLevel에서 바인딩 변수 사용하기 (0) | 2011.08.23 |