当你使用多线程时,比如你想用子线程执行一个耗时任务【比如说下载一个大文件】,在这个任务执行完之前,你还想接着干其他的事情【主线程响应其它事件】,然后当你需要这个子线程执行的结果时,拿到它。
我们知道,创建线程的方式有两种:一种是实现Runnable接口,另一种是继承Thread。但这两种方式都无法获取执行后的结果。但是通过Callable接口和Future接口,我们可以拿到执行后的结果。
Callable有返回值,我们可以通过FutureTask.get()来取得Callable返回的值。
如下代码:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class FutureTaskTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
spendTime st = new spendTime();
FutureTask<Integer> task = new FutureTask<Integer>(st);
new Thread(task).start();
for(int i = 0; i < 3; i ++)
{
System.out.println("my thread ---->"+ Thread.currentThread().getName()+", i is-----> " + i);
Thread.sleep(500);
}
System.out.println("task.get()----->"+task.get());
}
static class spendTime implements Callable<Integer>{
int sum = 0;
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
for(int i = 0; i < 5; i ++)
{
sum += i;
System.out.println("my thread is " + Thread.currentThread().getName() + ", i is ---->" + i);
Thread.sleep(1000);
}
System.out.println("sum is---->" + sum);
return sum;
}
}
}
运行结果:
my thread ---->main, i is-----> 0
my thread is Thread-0, i is ---->0
my thread ---->main, i is-----> 1
my thread ---->main, i is-----> 2
my thread is Thread-0, i is ---->1
my thread is Thread-0, i is ---->2
my thread is Thread-0, i is ---->3
my thread is Thread-0, i is ---->4
sum is---->10
task.get()----->10
可以看到,在启动子线程后,main线程会接着做自己的事情,这是理所当然的!然后我们可以看到最后一行:“task.get()--->”10.说明可以通过task.get()来取得线程的返回值,
接着我们把代码改一下,再运行:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class FutureTaskTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
spendTime st = new spendTime();
FutureTask<Integer> task = new FutureTask<Integer>(st);
new Thread(task).start();
System.out.println("task.get()----->"+task.get());
for(int i = 0; i < 3; i ++)
{
System.out.println("my thread ---->"+ Thread.currentThread().getName()+", i is-----> " + i);
Thread.sleep(500);
}
}
static class spendTime implements Callable<Integer>{
int sum = 0;
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
for(int i = 0; i < 5; i ++)
{
sum += i;
System.out.println("my thread is " + Thread.currentThread().getName() + ", i is ---->" + i);
Thread.sleep(1000);
}
System.out.println("sum is---->" + sum);
return sum;
}
}
}
运行结果为:
my thread is Thread-0, i is ---->0
my thread is Thread-0, i is ---->1
my thread is Thread-0, i is ---->2
my thread is Thread-0, i is ---->3
my thread is Thread-0, i is ---->4
sum is---->10
task.get()----->10
my thread ---->main, i is-----> 0
my thread ---->main, i is-----> 1
my thread ---->main, i is-----> 2
这回我把task.get()放在主线程for循环的前面,看到了啥?
对,先执行的是子线程,主线程在task.get()那里阻塞,等子线程执行完后,主线程才继续执行。
这个说明,task.get()这个方法会阻塞它后面的其它代码。如果子线程没执行完,task.get()阻塞会一直等待它执行结束,然后取得call的返回值。
所以我们应当在主线程中启动子线程去执行耗时操作,想要主线程继续执行的话,将主线程的执行代码放在task.get()的前面,当我们需要取得子线程执行结果时,再用task.get()取得。如果子线程没执行完,task.get()会一直等待!
FutureTask是Future的实现类,所以可以通过以下方法
boolean
cancel(
boolean
mayInterruptIfRunning);
boolean
isCancelled();
boolean
isDone();