The content this article brings to you is about the relationship and usage between Future and FutureTask in Java (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
Future and FutureTask are both used to obtain the return results of thread execution. Below we will give a general introduction and analysis of the relationship and use between the two
1. Future andIntroduction to FutureTask:
Future is located under the java.util.concurrent package, it is an interface
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 and FutureTaskUsage and analysis 1. When using Future, we need to implement the Callable interface and obtain the returned Future object through the submit method of the ExecutorService interface.
2. When using FutureTask, according to the constructor of FutureTask, we can see that FutureTask has both You can receive the implementation class of Callable or the implementation class of Runnable. When you pass in the implementation class of Callable, you can get the result of thread execution; when you pass in the implementation class of Runnable, since the implementation of Runnable has no return value, you need to pass in a thread completion identifier that you set, that is, result. Then when the thread ends, the original result value you passed in will be returned to you. The constructor of FutureTask is as follows:public class FutureTask<V> implements RunnableFuture<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);//runnable转化为callable this.state = NEW; // ensure visibility of callable } }
// 执行任务 实现Runnable FutureTaskJobRunnable taskRun = new FutureTaskJobRunnable(); // 执行任务 实现Callable FutureTaskJobCallable taskCall = new FutureTaskJobCallable(); String val = "ok"; // 线程运行成功后把,返回你传入的val值 FutureTask<String> futureTaskRun = new FutureTask<String>(taskRun, val); // 线程运行,返回线程执行的结果 FutureTask<String> futureTaskCall = new FutureTask<String>(taskCall); //声明线程池 ExecutorService executor = Executors.newCachedThreadPool(); //Future Future<String> future = executor.submit(taskCall); System.out.println(future.get()); //FutureTask executor.submit(futureTaskCall); System.out.println(futureTaskCall.get()); //FutureTask自定义线程执行 new Thread(futureTaskRun).start(); System.out.println(futureTaskRun.get());
public class FutureTaskJobCallable implements Callable<String>{ public String call() throws Exception { System.out.println("FutureTaskJobCallable已经执行了哦"); Thread.sleep(1000); return "返回结果"; } } public class FutureTaskJobRunnable implements Runnable { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("FutureTaskJobRunnable已经执行了哦"); } }
public Future<?> submit(Runnable task) { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor(task, null); execute(ftask); return ftask; } public <T> Future<T> submit(Runnable task, T result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task, result); execute(ftask); return ftask; } public <T> Future<T> submit(Callable<T> task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task); execute(ftask); return ftask; } protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { return new FutureTask<T>(callable); }
2、你传入的已经是个自己构造的FutureTask对象,由于FutureTask其实是实现了Runnable接口的,它本身就是个Runaable实现类, sumbit方法还是会将它视为Runnable类来进行封装,并最终会执行FutureTask自己的run方法,一系列实现都在你传入的FutureTask对象内完成,所以你可以直接通过自己构建的FutureTask获取结果;
3、自己单独声明线程运行,跟第2点类似,FutureTask本身就是个Runnabel实现类,自然可以做为参数传入Thread运行;
那么我们把自定义的Runnable、Callable实现类做为参数构造FutureTask后,FuttureTask是如何运行的呢,我们可以看下FuttureTask中具体的代码实现
//你传入的Runnable与Callable实现类都会在构造函数中转化为Callable private Callable<V> callable; public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; try { Callable<V> c = callable;//你传入的实现类 if (c != null && state == NEW) { V result;//返回值 boolean ran; try { result = c.call();//运行后返回结果 ran = true; } catch (Throwable ex) { result = null; ran = false; setException(ex); } if (ran) set(result); } } finally { // runner must be non-null until state is settled to // prevent concurrent calls to run() runner = null; // state must be re-read after nulling runner to prevent // leaked interrupts int s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } }
可以看到FutureTask类本身的run方法,就是执行Runnable、Callable的实现类并获取返回结果的过程。
所以ExecutorService接口中submit方法归根结底还是要把你传入的对象封装成FutureTask对象,并通过FutureTask类的内部实现来获取结果的,返回的Future接口对象也要依赖于FutureTask实例化的,所以无论是直接传入自己的Runnable、Callable实现类还是构建FutureTask传入,本质上都是通过FutureTask去实现,没有什么区别;
The above is the detailed content of The relationship and usage between Future and FutureTask in java (with code). For more information, please follow other related articles on the PHP Chinese website!