, T result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor 从上面的源码可以看到,这三者方法几乎是一样的,关键就在于: RunnableFuture<T> ftask = newTaskFor(task); execute(ftask); 它是如何将一个任务作为参数传递给了 newTaskFor,然后调用execute方法,最后进而返回ftask的呢? //AbstractExecutorService#newTaskFor protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { return new FutureTask<T>(callable); } protected <T> RunnableFuture<T> newTaskFor(Runnable runnable
newTaskFor 方法 private RunnableFuture<V> newTaskFor(Callable<V> task) { if (aes == null) return new FutureTask<V>(task); else return aes.newTaskFor(task); } private RunnableFuture<V> newTaskFor(Runnable task, V result) { if (aes == null) return new FutureTask<V>(task, result); else return aes.newTaskFor(task, result); } > task) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor
先说 submit() 方法,这个方法如果接受的是 Runnable 参数的话就直接调用了 newTaskFor(Runnable runnable, T value) ,在前面会看到对于 Runnable 然后另外一个传参为 callable 的就调用 newTaskFor(Callable<T> callable) 。 // 调用的都是 newTaskFor 的 Runnable 参数方法 public Future<? // 这两个是 AES 的核心方法,主要就是创建任务 是 submit 的依赖 protected <T> RunnableFuture<T> newTaskFor(Runnable runnable , T value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor
Callable<V> task) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor (task); } private RunnableFuture<V> newTaskFor(Runnable task, V result) { if (aes = = null) return new FutureTask<V>(task, result); else return aes.newTaskFor > task) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor result) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor
{ if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor (task, null); execute(ftask); return ftask; } 上面方法调用的 newTaskFor 方法返回了一个 FutureTask protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask
先说 submit() 方法,这个方法如果接受的是 Runnable 参数的话就直接调用了 newTaskFor(Runnable runnable, T value) ,在前面会看到对于 Runnable 然后另外一个传参为 callable 的就调用 newTaskFor(Callable<T> callable) 。 // 调用的都是 newTaskFor 的 Runnable 参数方法 public Future<? // 这两个是 AES 的核心方法,主要就是创建任务 是 submit 的依赖 protected <T> RunnableFuture<T> newTaskFor(Runnable runnable , T value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor
Runnable task) { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor (task, null); execute(ftask); return ftask;}submit中调用了newTaskFor方法来返回一个ftask对象,然后execute这个ftask 对象,newTaskFor代码如下:// AbstractExecutorService.newTaskForprotected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask<T>(runnable, value);}newTaskFor又调用我们熟悉的FutureTask的有参构造器来创建一个
{ if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor (task, null); execute(ftask); return ftask; } 上面方法调用的 newTaskFor 方法返回了一个 FutureTask protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask
> task) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor result) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor executor.execute(new QueueingFuture(f)); return f; } private RunnableFuture<V> newTaskFor return new FutureTask<V>(task, result); else //添加一个任务 return aes.newTaskFor
newTaskFor方法 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); } RunnableFuture类用于获取执行结果,在实际使用时,我们经常使用的是它的子类FutureTask,newTaskFor task, V result) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor Callable<T> t : tasks) { 将每个任务封装成RunnableFuture对象提交任务 RunnableFuture<T> f = newTaskFor
abstract class AbstractExecutorService implements ExecutorService { protected <T> RunnableFuture<T> newTaskFor value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor
{ if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor (task, null); execute(ftask); return ftask; } 不难发现提交任务是传入了ftask对象,看看newTaskFor() protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask
AbstractExecutorService除了实现ExecutorService接口外,还提供了newTaskFor()方法返回一个RunnableFuture,在运行的时候,它将调用底层可调用任务 AbstractExecutorService提供了newTaskFor()方法返回一个RunnableFuture,除此之外当我们把一个Runnable或者Callable提交给(submit())ThreadPoolExecutor 如下: protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask <T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
abstract class AbstractExecutorService implements ExecutorService { protected <T> RunnableFuture<T> newTaskFor value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor
这个抽象类实现了 invokeAny 方法和 invokeAll 方法,这里的两个 newTaskFor 方法也比较有用,用于将任务包装成 FutureTask。 方法用于将我们的任务包装成 FutureTask 提交到线程池中执行 protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, 将任务包装成 FutureTask RunnableFuture<Void> ftask = newTaskFor(task, null); // 2\. 将任务包装成 FutureTask RunnableFuture<T> ftask = newTaskFor(task, result); // 2\. 将任务包装成 FutureTask RunnableFuture<T> ftask = newTaskFor(task); // 2\.
abstract class AbstractExecutorService implements ExecutorService { protected <T> RunnableFuture<T> newTaskFor value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor
abstract class AbstractExecutorService implements ExecutorService { protected <T> RunnableFuture<T> newTaskFor value) { return new FutureTask<T>(runnable, value); } protected <T> RunnableFuture<T> newTaskFor { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor , T result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor
{ if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor (task, null); execute(ftask); return ftask; } 上面方法调用的 newTaskFor 方法返回了一个 FutureTask protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask
从 Java6 开始,ExecutorService 实现可以改写 AbstractExecutorService 中的 newTaskFor 方法,从而根据已提交的 Runnable 或 Callable 如下代码清单【AbstractExecutorService 中的 newTaskFor 方法的默认实现、submit 方法实现】:protected <T> RunnableFuture<T> newTaskFor runnable, T value) { return new FutureTask<T>(runnable, value);}protected <T> RunnableFuture<T> newTaskFor Runnable task) { if (task == null) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor task, T result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor
可以看到,执行到submit方法内部时,会将我们传进来的new MyCallable()对象作为参数传入到newTaskFor(task)方法里——public <T> Future<T> submit Callable<T> task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor (task); execute(ftask); return ftask;}这个newTaskFor(task)方法内部具体实现,是将new MyCallable()对象传入构造器中,生成了一个 protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { return new FutureTask<T>(callable Callable<T> task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor