java8线程池如何使用
1、线程池的特点是提供一个线程队列,队列中保存着所有等待状态的线程。避免了创建与销毁额外开销,提高了响应速度。
2、线程池的所有类在java.util.concurrent.Executor:负责线程的使用与调度的根接口
3、创建固定大小线程池
ExecutorService executorService = Executors.newFixedThreadPool(5);

4、为线程池的线程分配任务
executorService.submit(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("------------");
}
}));

5、关闭线程池
executorService.shutdown();

6、线程池执行Callable线程
Future<Integer> future = executorService.submit(new Callable<Integer>() {
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
return sum;
}
});

7、得到程序的返回结果
Integer sum = future.get();

8、在线程池中提交十个线程执行程序。
List<Future<Integer>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Future<Integer> future = executorService.submit(new Callable<Integer>() {
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
return sum;
}
});
list.add(future);
}
