'executroservice'에 해당되는 글 1건

  1. 2017.12.12 [Java] Executors 로 간단 multithread 테스트

[Java] Executors 로 간단 multithread 테스트

ITWeb/개발일반 2017. 12. 12. 11:54

이전 글의 참고 문서들을 먼저 보시면 좋습니다.

http://jjeong.tistory.com/1296

package hello.executors;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class HelloExecutors {

public static class HelloCallableThread implements Callable<Integer> {
int input;

public HelloCallableThread(int input) {
this.input = input;
}

@Override
public Integer call() throws Exception {
return input + 1;
}
}

public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(4);
Set<Callable<Integer>> callables = new HashSet<Callable<Integer>>();

callables.add(new HelloCallableThread(1));
callables.add(new HelloCallableThread(2));
callables.add(new HelloCallableThread(3));
callables.add(new HelloCallableThread(4));

List<Future<Integer>> futures = executorService.invokeAll(callables);

for(Future<Integer> future : futures){
System.out.println("future.get = " + future.get());
}

executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.SECONDS);
}
}


: