-
Callable과 FutureJAVA/JAVA8 관련 2022. 5. 28. 17:39
Callable
- Runnable과 유사하지만 작업의 결과를 받을 수 있다. (return)
Callabel 관련 메서드
get()
- 블록킹 콜이다.
- 타임아웃을 설정할 수 있다.
- 아래 예제의 경우 Started!!!을 출력하고 Callable 작업이 완료 된 이후(2초 Thread.sleep) End!!!를 출력한다.
isDone()
- 작업을 종료 했으면 true, 종료하지 못했으면 false를 리턴한다.
public class CallabeAndFuture { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () -> { Thread.sleep(2000L); return "Hello JUMP!!"; }; Future<String> stringFuture = executorService.submit(hello); System.out.println(stringFuture.isDone()); // 작업이 완료 되지 않았으므로 false System.out.println("Started!!!"); System.out.println(stringFuture.get()); //블록킹 콜, 작업이 완료될 때 까지 기다림.. System.out.println(stringFuture.isDone()); // 작업이 완료 됬음으로 false System.out.println("End!!!"); executorService.shutdown(); } }
cancel()
- 작업을 취소하는 메서드
- parameter로 true를 전달하면 현재 진행중인 쓰레드를 interrupt하고, 그러지 않으면 현재 진행중인 작업이 끝날때까지 기 다린다.
public class CallabeAndFuture2 { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () -> { Thread.sleep(2000L); return "Hello JUMP!!"; }; Future<String> stringFuture = executorService.submit(hello); System.out.println("isDone : " + stringFuture.isDone()); // 작업이 완료 되지 않았으므로 false System.out.println("Started!!!"); //블록킹 콜, 작업이 완료될 때 까지 기다림.. System.out.println("cancel : " +stringFuture.cancel(false)); System.out.println("isDone : " + stringFuture.isDone()); // 작업이 완료 됬음으로 true System.out.println(stringFuture.get()); System.out.println("End!!!"); executorService.shutdown(); } }
Thread 작업이 완료되기 전에 cancel하였기 때문에, 작업이 isDone() true인 것은 맞다. (종료가 됨)
하지만 get() 메서드를 통하여 완료된 작업을 가져오는 것은 불가능하다.
invokeAll()
- 여러 작업을 동시에 실행한다.
- 동시에 실행한 작업들 중에 가장 오래걸리는 작업 만큼 시간이 걸림.
public class CallabeAndFuture3 { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () -> { Thread.sleep(1000); return "HI!!!"; }; Callable<String> happy = () -> { Thread.sleep(2000); return "Happy"; }; Callable<String> newYear = () -> { Thread.sleep(3000L); return "new Year!!"; }; List<Future<String>> futures = executorService.invokeAll(Arrays.asList(hello, happy, newYear)); for (Future<String> f : futures) { System.out.println(f.get()); } executorService.shutdown(); } }
invokeAny()
- 여러작업 중 하나라도 먼저 응답이 오면 끝낸다.
- 동시에 실행한 작업 중에 제일 짧게 걸리는 작업만큼 시간이 걸림
- 블록킹 콜
public class CallabeAndFuture4 { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () -> { Thread.sleep(1000); return "HI!!!"; }; Callable<String> happy = () -> { Thread.sleep(2000); return "Happy"; }; Callable<String> newYear = () -> { Thread.sleep(3000L); return "new Year!!"; }; String first = executorService.invokeAny(Arrays.asList(hello, happy, newYear)); System.out.println(first); executorService.shutdown(); } }
'JAVA > JAVA8 관련' 카테고리의 다른 글
CompletableFuture2 (0) 2022.05.31 CompletableFuture1 (0) 2022.05.31 Concurrent - Executor (0) 2022.05.22 Concurrent 프로그래밍 소개 (0) 2022.05.20 Date와 Time (0) 2022.05.15