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()); }
相关文章推荐
- JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没
- JAVA多线程实现的三种方式(继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程)
- java创建多线程使用Executors创造ExecutorService
- 理解java多线程中ExecutorService使用
- java 使用ExecutorService 建立多线程
- java多线程核心接口 — ExecutorService 的理解与使用
- java创建多线程使用Executors创造ExecutorService
- Java多线程--ScheduledExecutorService的使用
- Java多线程:Callable、ExecutorService、CountDownLatch、Future和FutureTask
- Java多线程之Executor、ExecutorService、Executors、Callable、Future与FutureTask
- 使用ExecutorService、Callable、Future实现有返回结果的多线程 应用实例
- Java多线程--ExecutorService的使用
- JAVA多线程Callable、Executors -- ExecutorService简介
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- Java 定时任务 ScheduledExecutorService 的使用
- java多线程并发executorservice(任务调度)类
- Callable和Future,CompletionService和ExecutorCompletionService的好处和使用场景
- java使用ScheduledExecutorService定时任务
- 【Java多线程】-Timer,TimerTask,ScheduledExecutorService
- java利用线程池(ExecutorService)配合Callable和Future实现执行方法超时的阻断