Callable与Future
Callable与Runnable的区别
Runnable
封装了一个异步运行的任务,可以把他想象成一个没有参数和返回值的异步方法。
Callable
与Runnable
类似,但是有返回值。Callable
接口是一个参数化类型,只有一个方法call
。例如:
Callable<Integer>
,类型参数是返回值的类型。这里表示一个最终返回Integer
对象的异步计数
V call()
:运行一个任务,它将生成一个结果
Future
Future
保存异步计算的结果。
V get()
V get(long time,TimeUnit unit)
:获取这个方法会阻塞,直到结果可以或超过了指定的时间。不成功就抛出异常。
boolean cancel(boolean mayInterrupt)
:尝试取消这个任务的运行。如果任务没开始则永远不会执行,如果正在运行且mayInterrupt
参数为true
时,计算会被中断。
boolean isCancelled()
:如果任务在完成前被取消,则返回true
boolean isDone()
:如果任务在进行返回false
,如果完成则返回true
FutureTask
FutrueTask
,它实现了Future
和Runable
接口。
FutrueTask
可以执行Callable
任务。
FutureTask(Callable<V> task)
FutureTask(Runnable task,V result)
构造一个即是
Future<V>
又是Runnable
的对象。
Callable<Integer> task = ...;
var futureTask = new FutureTask<Integer>(task);
var t = new Thread(futureTask);
t.start();
....
Integer result = futureTask.get();
案例:
计算任务,一个包含了2万个整数的数组,分拆了多个线程来进行并行计算,最后汇总出计算的结果。
方法1:Callable+线程池
public class Task implements Callable<Integer> {
private int[] is;
private int start;
private int end;
public Task(int[] is, int start, int end) {
this.is = is;
this.start = start;
this.end = end;
}
//call()相当于Runnable中的run()
//线程抢到CPU资源后,才调用call()
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = start; i < end; i++) {
sum += is[i];
}
return sum;
}
}
public class Test01 {
public static void main(String[] args) throws InterruptedException, ExecutionException, ExecutionException {
//创建2万个整数的数组
int[] is = new int[20000];
//初始化数据(数组中的数据:1~20000)
for (int i = 0; i < is.length; i++) {
is[i] = i+1;
}
ExecutorService pool = Executors.newCachedThreadPool();
//创建任务
Task task1 = new Task(is, 0, 5000);
Task task2 = new Task(is, 5000, 10000);
Task task3 = new Task(is, 10000, 15000);
Task task4 = new Task(is, 15000, 20000);
//将带有返回值的任务对象提交给线程池,并返回Future对象,任务返回的数据是存放在Future对象中的
Future<Integer> future1 = pool.submit(task1);
Future<Integer> future2 = pool.submit(task2);
Future<Integer> future3 = pool.submit(task3);
Future<Integer> future4 = pool.submit(task4);
//将任务的结果取出,并合并计算
int result = future1.get() + future2.get() + future3.get() + future4.get();
System.out.println(result);
pool.shutdown();
}
}
方法2:Callable+FutrueTask
public class Task implements Callable<Integer> {
private int[] is;
private int start;
private int end;
public Task(int[] is, int start, int end) {
this.is = is;
this.start = start;
this.end = end;
}
//call()相当于Runnable中的run()
//线程抢到CPU资源后,才调用call()
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = start; i < end; i++) {
sum += is[i];
}
return sum;
}
}
public class Test02 {
public static void main(String[] args) throws InterruptedException, ExecutionException, ExecutionException {
//创建2万个整数的数组
int[] is = new int[20000];
//初始化数据(数组中的数据:1~20000)
for (int i = 0; i < is.length; i++) {
is[i] = i+1;
}
//创建任务
Callable<Integer> task1 = new Task(is, 0, 5000);
Callable<Integer> task2 = new Task(is, 5000, 10000);
Callable<Integer> task3 = new Task(is, 10000, 15000);
Callable<Integer> task4 = new Task(is, 15000, 20000);
FutureTask<Integer> f1 = new FutureTask<>(task1);
FutureTask<Integer> f2 = new FutureTask<>(task2);
FutureTask<Integer> f3 = new FutureTask<>(task3);
FutureTask<Integer> f4 = new FutureTask<>(task4);
Thread t1 = new Thread(f1);
Thread t2 = new Thread(f2);
Thread t3 = new Thread(f3);
Thread t4 = new Thread(f4);
t1.start();
t2.start();
t3.start();
t4.start();
//将任务的结果取出,并合并计算
int result = f1.get() + f2.get() + f3.get() + f4.get();
System.out.println(result);
}
}
方法3:Runnable
public class Task implements Runnable{
private int sum=0;
private int from;
private int to;
private final int [] arr;
public Task(int[] a, int f, int t){
arr = a;
this.from = f;
this.to = t;
}
@Override
public void run() {
for (int i = from; i < to; i++) {
sum = sum+arr[i];
}
}
public int getSum() {
return sum;
}
}
import java.util.Arrays;
public class Work02 {
public static void main(String[] args) throws InterruptedException {
int arr [] = new int[20000];
Arrays.fill(arr,1);
Task t1 = new Task(arr, 0, 5000);
Task t2 = new Task(arr, 5000, 10000);
Task t3 = new Task(arr, 10000, 15000);
Task t4 = new Task(arr, 15000, 20000);
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
Thread thread3 = new Thread(t3);
Thread thread4 = new Thread(t4);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread1.join();
thread2.join();
thread3.join();
thread4.join();
int sum = t1.getSum()+t2.getSum()+t3.getSum()+t4.getSum();
System.out.println("汇总:"+sum);
}
}