在Java多线程编程中,创建启动一个线程可以继承Thread类,也可以实现Runnable接口,但是想要获取线程的结果,则需要实现Callable接口,获取结果则需要使用Future接口。下面分别介绍这几个接口或者类的实现。
Runnable接口
Runnable接口只有一个run方法,其线程执行逻辑在run方法中实现。但是该方法没有返回值,所以不能获得线程执行的结果。其接口定义如下:
- public interface Runnable {
- /**
- * When an object implementing interface <code>Runnable</code> is used
- * to create a thread, starting the thread causes the object's
- * <code>run</code> method to be called in that separately executing
- * thread.
- * <p>
- *
- * @see java.lang.Thread#run()
- */
- public abstract void run();
- }
Callable接口
Callable接口和Runnable接口很相似,但是其call方法有返回值,所以可以获取线程执行的返回值。
- public interface Callable<V> {
- /**
- * Computes a result, or throws an exception if unable to do so.
- *
- * @return computed result
- * @throws Exception if unable to compute a result
- */
- V call() throws Exception;
- }
Future接口
Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作。其get方法会阻塞,
直到超时,或者获得任务执行结果。
- /**
- * @see FutureTask
- * @see Executor
- * @since 1.5
- * @author Doug Lea
- * @param <V> The result type returned by this Future's <tt>get</tt> method
- */
- public interface Future<V> {
- /**
- * Attempts to cancel execution of this task. This attempt will
- * fail if the task has already completed, has already been cancelled,
- * or could not be cancelled for some other reason. If successful,
- * and this task has not started when <tt>cancel</tt> is called,
- * this task should never run. If the task has already started,
- * then the <tt>mayInterruptIfRunning</tt> parameter determines
- * whether the thread executing this task should be interrupted in
- * an attempt to stop the task. *
- */
- boolean cancel(boolean mayInterruptIfRunning);
- /**
- * Returns <tt>true</tt> if this task was cancelled before it completed
- * normally.
- */
- boolean isCancelled();
- /**
- * Returns <tt>true</tt> if this task completed.
- *
- */
- boolean isDone();
- /**
- * Waits if necessary for the computation to complete, and then
- * retrieves its result.
- *
- * @return the computed result
- */
- V get() throws InterruptedException, ExecutionException;
- /**
- * Waits if necessary for at most the given time for the computation
- * to complete, and then retrieves its result, if available.
- *
- * @param timeout the maximum time to wait
- * @param unit the time unit of the timeout argument
- * @return the computed result
- */
- V get(long timeout, TimeUnit unit)
- throws InterruptedException, ExecutionException, TimeoutException;
- }
FutureTask
FutureTask是一个RunnableFuture<V>接口实现,而RunnableFuture<V>实现了Runnable和Future两个接口,同时,它还可以包装
Runnable和Callable接口,由构造函数注入:
- 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
- }
由于FutureTask实现了Runnable接口和Future接口,同时包装了Callable,所以它既可以传递给Thread类来执行,也可以提交给
ExecuteService来执行。并且还可以直接通过get函数来获取执行结果。