众所周知创建线程的方式有两种:1.继承Thread类。2.实现Runnable接口。从jdk1.5开始,提供了另一种创建线程的方式。今天我们就来看看这第三种方式:实现Callable接口
一、Callable与Runnable
我们直接来看一个使用Callable创建线程的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 1/*
2 * 一、创建执行线程的方式三:实现 Callable 接口。 相较于实现 Runnable 接口的方式,方法可以有返回值,并且可以抛出异常。
3 *
4 * 二、执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。 FutureTask 是 Future 接口的实现类
5 */
6public class TestCallable {
7
8 public static void main(String[] args) {
9 ThreadDemo td = new ThreadDemo();
10
11 //1.执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。
12 FutureTask<Integer> result = new FutureTask<>(td);
13
14 new Thread(result).start();
15
16 //2.接收线程运算后的结果
17 try {
18 Integer sum = result.get(); //FutureTask 可用于 闭锁
19 System.out.println(sum);
20 System.out.println("------------------------------------");
21 } catch (InterruptedException | ExecutionException e) {
22 e.printStackTrace();
23 }
24 }
25
26}
27
28class ThreadDemo implements Callable<Integer>{
29
30 @Override
31 public Integer call() throws Exception {
32 int sum = 0;
33
34 for (int i = 0; i <= 100000; i++) {
35 sum += i;
36 }
37
38 return sum;
39 }
40
41}
42
43
从这个例子可以看出,Callable与Runnable的主要区别在于Callable可以拿到任务执行后的返回值。Callable位于java.util.concurrent包下,它也是一个接口,在它里面也只声明了一个方法,只不过这个方法叫做call():
1
2
3
4
5
6
7
8
9
10
11 1public interface Callable<V> {
2 /**
3 * Computes a result, or throws an exception if unable to do so.
4 *
5 * @return computed result
6 * @throws Exception if unable to compute a result
7 */
8 V call() throws Exception;
9}
10
11
可以看到,这是一个泛型接口,call()函数返回的类型就是传递进来的V类型。
那么怎么使用Callable呢?一般情况下是配合ExecutorService来使用的,在ExecutorService接口中声明了若干个submit方法的重载版本:
1
2
3
4
5 1<T> Future<T> submit(Callable<T> task);
2<T> Future<T> submit(Runnable task, T result);
3Future<?> submit(Runnable task);
4
5
第一个submit方法里面的参数类型就是Callable。
一般情况下我们使用第一个submit方法和第三个submit方法,第二个submit方法很少使用。
二、Future和FutureTask
Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果。必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果。
Future类位于java.util.concurrent包下,它是一个接口:
1
2
3
4
5
6
7
8
9
10 1public interface Future<V> {
2 boolean cancel(boolean mayInterruptIfRunning);
3 boolean isCancelled();
4 boolean isDone();
5 V get() throws InterruptedException, ExecutionException;
6 V get(long timeout, TimeUnit unit)
7 throws InterruptedException, ExecutionException, TimeoutException;
8}
9
10
在Future接口中声明了5个方法,下面依次解释每个方法的作用:
- cancel方法用来取消任务,如果取消任务成功则返回true,如果取消任务失败则返回false。参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。如果任务已经完成,则无论mayInterruptIfRunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;如果任务正在执行,若mayInterruptIfRunning设置为true,则返回true,若mayInterruptIfRunning设置为false,则返回false;如果任务还没有执行,则无论mayInterruptIfRunning为true还是false,肯定返回true。
- isCancelled方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true。
- isDone方法表示任务是否已经完成,若任务完成,则返回true;
- get()方法用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回;
- get(long timeout, TimeUnit unit)用来获取执行结果,如果在指定时间内,还没获取到结果,就会抛出异常,如果捕获了直接返回null。
也就是说Future提供了三种功能
1)判断任务是否完成;
2)能够中断任务;
3)能够获取任务执行结果
因为Future只是一个接口,所以是无法直接用来创建对象使用的,因此就有FutureTask。
我们先来看一下FutureTask的实现:
1
2
3 1public class FutureTask<V> implements RunnableFuture<V>
2
3
FutureTask类实现了RunnableFuture接口,我们看一下RunnableFuture接口的实现:
1
2
3
4
5 1public interface RunnableFuture<V> extends Runnable, Future<V> {
2 void run();
3}
4
5
可以看出RunnableFuture继承了Runnable接口和Future接口,而FutureTask实现了RunnableFuture接口。所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值。
FutureTask提供了2个构造器:
1
2
3
4
5
6 1public FutureTask(Callable<V> callable) {
2}
3public FutureTask(Runnable runnable, V result) {
4}
5
6
事实上,FutureTask是Future接口的一个唯一实现类。
下面来一个FutureTaskde使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 1public class Test {
2 public static void main(String[] args) {
3 //第一种方式
4 ExecutorService executor = Executors.newCachedThreadPool();
5 Task task = new Task();
6 FutureTask<Integer> futureTask = new FutureTask<Integer>(task);
7 executor.submit(futureTask);
8 executor.shutdown();
9
10 //第二种方式,注意这种方式和第一种方式效果是类似的,只不过一个使用的是ExecutorService,一个使用的是Thread
11 /*Task task = new Task();
12 FutureTask<Integer> futureTask = new FutureTask<Integer>(task);
13 Thread thread = new Thread(futureTask);
14 thread.start();*/
15
16 try {
17 Thread.sleep(1000);
18 } catch (InterruptedException e1) {
19 e1.printStackTrace();
20 }
21
22 System.out.println("主线程在执行任务");
23
24 try {
25 System.out.println("task运行结果"+futureTask.get());
26 } catch (InterruptedException e) {
27 e.printStackTrace();
28 } catch (ExecutionException e) {
29 e.printStackTrace();
30 }
31
32 System.out.println("所有任务执行完毕");
33 }
34}
35class Task implements Callable<Integer>{
36 @Override
37 public Integer call() throws Exception {
38 System.out.println("子线程在进行计算");
39 Thread.sleep(3000);
40 int sum = 0;
41 for(int i=0;i<100;i++)
42 sum += i;
43 return sum;
44 }
45}
46
47
参考文章
- Java并发编程:Callable、Future和FutureTask
本文作者: catalinaLi
本文链接: http://catalinali.top/2018/helloCallable/