首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏余林丰

    13.ThreadPoolExecutor线程池之submit方法

    , 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

    2.9K100发布于 2018-01-12
  • 来自专栏java 成神之路

    ExecutorCompletionService 源码分析

    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

    75880发布于 2018-05-18
  • 来自专栏Java 源码分析

    Exectors框架 源码分析

    先说 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

    73970发布于 2018-04-17
  • 来自专栏测试基础

    【Java多线程-4】CompletionService详解

    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

    95020发布于 2020-09-16
  • 来自专栏小小码农一个。

    为什么要用线程池

    { 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

    53310发布于 2020-06-08
  • 来自专栏Java 源码分析

    Exectors框架 源码分析

    先说 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

    70860发布于 2018-04-17
  • 面试官:线程池提交一个任务占多大内存?

    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的有参构造器来创建一个

    42410编辑于 2024-12-08
  • 来自专栏小小码农一个。

    线程池几个常见的对比

    { 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

    53020发布于 2020-06-08
  • 来自专栏程序员备忘录

    ExecutorCompletionService源码学习

    > 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

    67220发布于 2020-08-25
  • 来自专栏冰河技术

    高并发之——P8级别架构师带你深度解析线程池中那些重要的顶层接口和抽象类

    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

    64210发布于 2020-10-29
  • 来自专栏彤哥读源码

    死磕 java线程系列之线程池深入解析——体系结构

    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

    59630发布于 2019-10-15
  • 来自专栏java小记

    java多线程学习(3)-线程池

    { 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

    43030发布于 2018-07-25
  • 来自专栏chenssy

    【死磕Java并发】-----J.U.C之线程池:线程池的基础架构

    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) {

    79150发布于 2018-04-26
  • 来自专栏以Java架构赢天下

    Java线程池ThreadPoolExecutor源码分析

    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

    58930发布于 2021-02-01
  • 来自专栏微信公众号【Java技术江湖】

    深度解读 java 线程池设计思想及源码实现

    这个抽象类实现了 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\.

    80720发布于 2019-09-25
  • 来自专栏小怪聊职场

    Java多线程之线程池(ThreadPoolExecutor)实现原理分析(一)

    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

    96370发布于 2018-05-21
  • 来自专栏编程从踩坑到跳坑

    ThreadPoolExecutor源码分析

    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

    51210发布于 2019-12-20
  • 来自专栏Vincent-yuan

    线程池之ThreadPoolExecutor使用(2)

    { 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

    61840发布于 2021-07-23
  • 来自专栏开发语言-Java

    Java并发编程学习11-任务执行演示

    从 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

    32411编辑于 2024-11-12
  • 来自专栏编程技术沉思录

    ExecutorService、Callable、Future实现有返回结果的多线程原理解析

    可以看到,执行到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

    1.2K10编辑于 2022-09-25
领券