今天我们通过实例来学习一下ExecutorService的用法。我徒然学会了抗拒热闹,却还来不及透悟真正的冷清。
ExecutorService的简单实例
一、ExecutorService的简单使用例子
public class ExecutorServiceTest { public static void main(String[] args) throws Exception { ExecutorService service = Executors.newSingleThreadExecutor(); Futurefuture = service.submit(new Callable () { @Override public String call() throws Exception { TimeUnit.SECONDS.sleep(5); return "string " + System.currentTimeMillis(); } }); System.out.println(future.get()); service.shutdown(); System.out.println("future的get()方法是阻塞的。"); } }
运行的结果如下:
string 1501051880989 future的get()方法是阻塞的。
二、ExecutorService中的invokeAny方法实例
package com.linux.huhx.thread; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; /** * Created by huhx on 2017-05-25. */ public class ExecuteServiceTest { public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); List> list = new ArrayList<>(); list.add(new MyThreadA()); list.add(new MyThreadB()); String string = executorService.invokeAny(list); System.out.println(string); executorService.shutdown(); } static class MyThreadA implements Callable { @Override public String call() throws Exception { System.out.println("begin: " + Thread.currentThread().getName() + ", " + System.currentTimeMillis()); TimeUnit.SECONDS.sleep(2); System.out.println("end: " + Thread.currentThread().getName() + ", " + System.currentTimeMillis()); return "Return A"; } } static class MyThreadB implements Callable { @Override public String call() throws Exception { System.out.println("begin: " + Thread.currentThread().getName() + ", " + System.currentTimeMillis()); TimeUnit.SECONDS.sleep(5); System.out.println("end: " + Thread.currentThread().getName() + ", " + System.currentTimeMillis()); return "Return B"; } } }
运行的效果如下:
修改main方法的代码如下:
String string = executorService.invokeAny(list, 2L, TimeUnit.SECONDS);
一次的运行的效果如下:
增大超时时间,代码如下:
String string = executorService.invokeAny(list, 3L, TimeUnit.SECONDS);
对于这个方法官方文档的介绍如下:
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.
三、ExecutorService中的invokeAny方法实例
沿用上述的两个线程的代码,调整main方法的代码如下:
public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); List> list = new ArrayList<>(); list.add(new MyThreadA()); list.add(new MyThreadB()); List > futures = executorService.invokeAll(list); for (Future stringFuture : futures) { System.out.println(stringFuture.get()); } executorService.shutdown(); }
运行的效果如下:
修改代码如下:
List> futures = executorService.invokeAll(list, 1L, TimeUnit.SECONDS); // 在for循环的外面添加以下的代码 System.out.println("hello world"); // 在main线程中
运行的效果如下:
在上述的基本上,修改超时的时间为3s。修改代码如下:
List> futures = executorService.invokeAll(list, 3L, TimeUnit.SECONDS);
修改超时时间>5s。修改的代码如下:
可以看到Future的get方法是阻塞的。关于这个invokeAll方法官方文档的说明如下:
Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. Future.isDone() is true for each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.
友情链接