标签:catch 学习 sleep stat bst hat 获得 com system
public interface Runnable {
public abstract void run();
}
Runnable的代码非常简单,它是一个接口且只有一个run(),创建一个类实现它,把一些费时操作写在其中,然后使用某个线程去执行该Runnable实现类即可实现多线程。
public interface Callable<V> {
V call() throws Exception;
}
Callable的代码也非常简单,不同的是它是一个泛型接口,call()函数返回的类型就是创建Callable传进来的V类型。
学习Callable对比着Runnable,这样就很快能理解它。Callable与Runnable的功能大致相似,Callable功能强大一些,就是被线程执行后,可以返回值,并且能抛出异常。
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
Future是一个接口,定义了Future对于具体的Runnable或者Callable任务的执行结果进行取消、查询任务是否被取消,查询是否完成、获取结果。
class MyCallable implements Callable<String>{
@Override
public String call() throws Exception {
System.out.println("做一些耗时的任务...");
Thread.sleep(5000);
return "OK";
}
}
public class FutureSimpleDemo {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newCachedThreadPool();
Future<String> future = executorService.submit(new MyCallable());
System.out.println("dosomething...");
System.out.println("得到异步任务返回结果:" + future.get());
System.out.println("Completed!");
}
}
上面是Future基本用法的代码以及并运行,我们可以知道:
也就是说,总结一句话,Future可以得到别的线程任务方法的返回值。
FutureTask的父类是RunnableFuture,而RunnableFuture继承了Runnbale和Futrue这两个接口
public class FutureTask<V> implements RunnableFuture<V> public interface RunnableFuture<V> extends Runnable, Future<V>
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
在这里我们可以了解到:
接下来我们看看Executors.callable方法代码
public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);
}
代码很简单,直接返回一个RunnableAdapter实例。
接下来我们看看RunnableAdapter方法代码
/**
* A callable that runs given task and returns given result
*/
static final class RunnableAdapter<T> implements Callable<T> {
final Runnable task;
final T result;
RunnableAdapter(Runnable task, T result) {
this.task = task;
this.result = result;
}
public T call() {
task.run();
return result;
}
}
可以了解到:
public class CallableAndFuture {
public static void main(String[] args) {
Callable<Integer> callable = new Callable<Integer>() {
public Integer call() throws Exception {
return new Random().nextInt(100);
}
};
FutureTask<Integer> future = new FutureTask<Integer>(callable);
new Thread(future).start();
try {
Thread.sleep(3000);// 可能做一些事情
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值,那么这个组合的使用有什么好处呢?假设有一个很费时逻辑需要计算并且返回这个值,同时这个值不是马上需要,那么就可以使用这个组合,用另一个线程去计算返回值,而当前线程在使用这个返回值之前可以做其它的操作,等到需要这个返回值时,再通过Future得到!
java学习之Runnable, Callable, Future, FutureTask
标签:catch 学习 sleep stat bst hat 获得 com system
原文地址:http://www.cnblogs.com/NewMan13/p/7738891.html