public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () ->{ Thread.sleep(2000L); return "Hello"; }; // submit 메소드 - <T> Future<T> submit(Callable<T> task); Future<String> helloFuture = executorService.submit(hello); System.out.println("Started!"); helloFuture.get(); // 블록킹 콜 System.out.println("End!"); executorService.shutdown(); }
public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () ->{ Thread.sleep(2000L); return "Hello"; }; // submit 메소드 - <T> Future<T> submit(Callable<T> task); Future<String> helloFuture = executorService.submit(hello); System.out.println("Start!, isDone? :" + helloFuture.isDone()); helloFuture.get(); // 블록킹 콜 System.out.println("End!, isDone? :" + helloFuture.isDone()); executorService.shutdown(); // 출력 // Start!, isDone? :false // (2초 후) - 블록킹 콜 // End!, isDone? :true }
public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> hello = () ->{ Thread.sleep(2000L); return "Hello"; }; // submit 메소드 - <T> Future<T> submit(Callable<T> task); Future<String> helloFuture = executorService.submit(hello); System.out.println("Start!, isDone? :" + helloFuture.isDone()); helloFuture.cancel(true); // cancel한 후 get을 하게 되면 java.util.concurrent.CancellationException이 발생한다. // helloFuture.get(); System.out.println("End!, isDone? :" + helloFuture.isDone()); executorService.shutdown(); // 출력 // Start!, isDone? :false // End!, isDone? :true (cancel을 하게 되면 isDone은 ture를 반환한다.) }
public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); // ExecutorService executorService = Executors.newFixedThreadPool(4); Callable<String> test1 = () ->{ Thread.sleep(1000L); return "Test1"; }; Callable<String> test2 = () ->{ Thread.sleep(2000L); return "Test2"; }; Callable<String> test3 = () ->{ Thread.sleep(3000L); return "Test3"; }; Callable<String> test4 = () ->{ Thread.sleep(4000L); return "Test4"; }; long start = System.currentTimeMillis(); List<Future<String>> list = executorService.invokeAll(Arrays.asList(test1, test2, test3, test4)); long end = System.currentTimeMillis(); for(Future<String> future : list){ System.out.println(future.get()); } System.out.println("경과 : " + (end - start) / 1000); // 출력 // Test1 // Test2 // Test3 // Test4 // 경과 : 10 // 만약 스레드가 4개라면 4초가 걸린다. executorService.shutdown(); }
public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(1); Callable<String> test2 = () ->{ Thread.sleep(2000L); return "Test2"; }; Callable<String> test4 = () ->{ Thread.sleep(4000L); return "Test4"; }; Callable<String> test1 = () ->{ Thread.sleep(1000L); return "Test1"; }; Callable<String> test3 = () ->{ Thread.sleep(3000L); return "Test3"; }; long start = System.currentTimeMillis(); String s = executorService.invokeAny(Arrays.asList(test1, test2, test3, test4)); long end = System.currentTimeMillis(); System.out.println(s); System.out.println("경과 : " + (end - start) / 1000); // 출력 // Test1 // 경과 : 1 executorService.shutdown(); }
더 자바, Java 8 - 인프런
자바 8에 추가된 기능들은 자바가 제공하는 API는 물론이고 스프링 같은 제 3의 라이브러리 및 프레임워크에서도 널리 사용되고 있습니다. 이 시대의 자바 개발자라면 반드시 알아야 합니다. 이
www.inflearn.com
Callable과 Future
Callable
Future
get()
isDone()
cancel()
invokeAll()
invokeAny()
더 자바, Java 8 - 인프런
자바 8에 추가된 기능들은 자바가 제공하는 API는 물론이고 스프링 같은 제 3의 라이브러리 및 프레임워크에서도 널리 사용되고 있습니다. 이 시대의 자바 개발자라면 반드시 알아야 합니다. 이
www.inflearn.com
'기록 > JAVA' 카테고리의 다른 글