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");
}
}
'Java' 카테고리의 다른 글
자바 Async1 (0) | 2020.11.08 |
---|---|
자바 Callback (0) | 2020.11.03 |
자바 newCachedThreadPool , newFixedThreadPool (0) | 2020.11.03 |
자바 ExecutorService2 (0) | 2020.11.03 |
자바 ExecutorService (0) | 2020.11.03 |