사용자 도구

사이트 도구


java:concurrent:executorservice

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:concurrent:executorservice [2018/03/06 10:42]
kwon37xi [ThreadPoolExecutor]
java:concurrent:executorservice [2020/08/07 13:53] (현재)
kwon37xi
줄 58: 줄 58:
   * 대표적인 Thread Pool?   * 대표적인 Thread Pool?
   * [[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html|ThreadPoolExecutor]]   * [[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html|ThreadPoolExecutor]]
-  * Spring의 [[http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.html|ThreadPoolTaskExecutor]]가 사용한다.+  * [[springframework:async|Spring @Async]]가 사용한다.
   * ''corePoolSize'' : 기본 Pool Size   * ''corePoolSize'' : 기본 Pool Size
   * ''maximumPoolSize'' : 최대 Pool Size   * ''maximumPoolSize'' : 최대 Pool Size
줄 67: 줄 67:
 > corePoolSize 만큼의 쓰레드가 만들어져 있으면, 그 다음 쓰레드는 queueCapacity만큼 큐에 쌓여 있다가 corePool로 실행이 인입된다. 따라서 corePoolSize가 작고 queueCapacity가 매우 크면 queue가 꽉차기 전까지는 실제로 쓰레드 풀이 확장되지 않고 계속해서 corePool 만 재사용하게 된다. > corePoolSize 만큼의 쓰레드가 만들어져 있으면, 그 다음 쓰레드는 queueCapacity만큼 큐에 쌓여 있다가 corePool로 실행이 인입된다. 따라서 corePoolSize가 작고 queueCapacity가 매우 크면 queue가 꽉차기 전까지는 실제로 쓰레드 풀이 확장되지 않고 계속해서 corePool 만 재사용하게 된다.
  
-  * reject : New tasks submitted in method execute(Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturatedIn either case, the execute method invokes the RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor) method of its RejectedExecutionHandler+  * reject : Executor가 shutdown 상태이거나, queue와 maximum size가 정해져 있을 경우 이것이 꽉 차면 ''execute(Runnable)''가 reject 된다이 때  ''RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor)''가 호출된다. 
 +  * ''corePoolSize=0'', ''maximumPoolSize=Integer.MAX_VALUE'', ''keepAliveTime=60sec'', **''queueCapacity=0''** 으로 지정하면 ''Executors.newCachedThreadPool()'' 설정. 
 + 
 +===== ExecutorCompletionService ===== 
 +  * [[https://dzone.com/articles/executorcompletionservice|ExecutorCompletionService in Practice - DZone Java]] 
 +  * 여러개의 ''Future''를 submit 한 상황에서 빨리 실행되는대로 값을 반환받아 사용할 수 있게 해준다. 
 +  * [[java:8:completable_future|Java 8 CompletableFuture]]를 사용한다면 불필요해보인다. 
 + 
 +<code java> 
 +final ExecutorService pool = Executors.newFixedThreadPool(5); 
 + 
 +// 응답 타입(String) 명시 필요 
 +final ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(pool); 
 + 
 +for (final String site : topSites) { 
 +    completionService.submit(new Callable<String>() { 
 +        @Override 
 +        public String call() throws Exception { 
 +            return IOUtils.toString(new URL("http://" + site), StandardCharsets.UTF_8); 
 +        } 
 +    }); 
 +
 + 
 +// submit 한 갯수가 정확해야한다. 
 +for(int i = 0; i < topSites.size(); ++i) { 
 +    final Future<String> future = completionService.take(); // 먼저 실행되는 대로 바로 리턴 
 +    try { 
 +        final String content = future.get(); 
 +        //...process contents 
 +    } catch (ExecutionException e) { 
 +        log.warn("Error while downloading", e.getCause()); 
 +    } 
 +
 +</code>
java/concurrent/executorservice.1520302369.txt.gz · 마지막으로 수정됨: 2018/03/06 10:42 저자 kwon37xi