Java
자바 ExecutorService3(Callable)
NaHyungMin
2020. 11. 3. 17:08
Callable로 해당 작업을 만들고 매개변수로 호출하는 형식.
package main.java.com;
import java.util.concurrent.*;
public class ExecutorServiceMain3 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("start");
ExecutorService executorService = Executors.newCachedThreadPool();
Callable<Integer> task = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int sum = 0;
for(int i = 0; i < 10; i++) {
sum += i;
}
return sum;
}
};
Future<Integer> future = executorService.submit(task);
int sum = future.get();
System.out.println(sum);
System.out.println("end");
}
}