您的位置:首页 > 编程语言 > Java开发

java 中多线程使用ExecutorService 和Callable

2017-10-06 16:44 423 查看
public static void main(String[] args) {
List<Dog> dogs = new ArrayList();
dogs.add(new Dog(1,2));
ExecutorService executorService = new ScheduledThreadPoolExecutor(2);
List<Callable<Integer>> callables = new ArrayList<>();
//()->dogs.get(0).sum() 将产生一个Callable<Integer> 对象,Integer 是函数sum的返回值
callables.add((()->dogs.get(0).sum()));
callables.add((()->dogs.get(0).add()));
try {
executorService.invokeAll(callables);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("submit test");
// 触发一个线程执行
executorService.submit(callables.get(0));
System.out.println("submit over");
executorService.shutdown();
//jvm 的处理器
System.out.println(Runtime.getRuntime().availableProcessors());

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐