【源码解析】聊聊线程池 实现原理与源码深度解析(二)

AbstractExecutorService

上一篇文章中,主要介绍了AbstractExecutorService的线程执行的核心流程,execute() 这个方法显然是没有返回执行任务的结果,如果我们需要获取任务执行的结果,怎么办?

Callable 就是一个可以获取线程执行的结果。

public abstract class AbstractExecutorService implements ExecutorService {/** 将任务包装成FutureTask任务。带返回值参数的*/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);}/*** @throws RejectedExecutionException {@inheritDoc}* @throws NullPointerException       {@inheritDoc}*/public Future<?> submit(Runnable task) {if (task == null) throw new NullPointerException();//1.将任务包装成RunableFuture对象,由于RunnableFuture是实现Runable类,所以execute的参数是一个可拓展的类型RunnableFuture<Void> ftask = newTaskFor(task, null);//2,交给具体的执行器进行实现execute(ftask);return ftask;}/*** @throws RejectedExecutionException {@inheritDoc}* @throws NullPointerException       {@inheritDoc}*/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;}/*** @throws RejectedExecutionException {@inheritDoc}* @throws NullPointerException       {@inheritDoc}*/public <T> Future<T> submit(Callable<T> task) {if (task == null) throw new NullPointerException();//将任务装成成一个FutureTask任务RunnableFuture<T> ftask = newTaskFor(task);//执行任务execute(ftask);return ftask;}}

submit其实是一个重载的方法,分别是一个task,以及可以传递获取结果的任务,以及使用callable。

demo

从源码上看三个方法其实都是将任务进行了封装,然后调用线程池执行的核心方法

    public static void main(String[] args) throws ExecutionException, InterruptedException {Callable<Integer> resultCallable = new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return 1 + 1;}};ExecutorService threadPool = Executors.newFixedThreadPool(1);Future<Integer> resultTask = threadPool.submit(resultCallable);System.out.println(resultTask.get());threadPool.shutdown();}

FutureTask

public class FutureTask<V> implements RunnableFuture<V> {/* NEW -> COMPLETING -> NORMAL* NEW -> COMPLETING -> EXCEPTIONAL* NEW -> CANCELLED* NEW -> INTERRUPTING -> INTERRUPTED*/private volatile int state;private static final int NEW          = 0; // 初始化状态private static final int COMPLETING   = 1; // 结果计算完成或响应中断到赋值给返回值的状态private static final int NORMAL       = 2; // 任务正常完成,结果被setprivate static final int EXCEPTIONAL  = 3; // 任务抛出异常private static final int CANCELLED    = 4; // 任务被取消private static final int INTERRUPTING = 5; // 线程中断状态被设置为true 线程未响应中断private static final int INTERRUPTED  = 6; // 线程已被中断/** The underlying callable; nulled out after running */private Callable<V> callable; // 需要执行的任务/** The result to return or exception to throw from get() */// 执行callable的线程,调用FutureTask.run()方法通过CAS设置private Object outcome; // non-volatile, protected by state reads/writes/** The thread running the callable; CASed during run() */// 执行callable的线程,调用FutureTask.run()方法通过CAS设置private volatile Thread runner;/** Treiber stack of waiting threads */private volatile WaitNode waiters;public FutureTask(Callable<V> callable) {if (callable == null)throw new NullPointerException();this.callable = callable;this.state = NEW; // 初始化状态是new      // ensure visibility of callable}
}
 /* 继承了Runnable ,因为线程池中执行的也是Runnbale的任务*/
public interface RunnableFuture<V> extends Runnable, Future<V> {/*** Sets this Future to the result of its computation* unless it has been cancelled.*/void run();
}

FutureTask 实现RunnableFuture,也间接实现了run方法。

重点

我们知道 execute(ftask); 本质就是利用线程池进行执行,而线程执行的时候,其实就是启动对应任务的run方法。

task.run();
		// 这里是什么时候调用的,其实是// execute(ftask)传入的任务 task.run()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 {//核心,执行任务的call方法,你看就是调用普通的方法一样。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 interruptsint s = state;//响应中断if (s >= INTERRUPTING)handlePossibleCancellationInterrupt(s);}}
  • 判断当前任务状态,非NEW直接返回
  • 执行对应c.call() 其实就是执行callable中的call方法。
  • 将返回值set进去
    protected void set(V v) {//CAS 去设置当前任务执行状态 new-completingif (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {//返回结果outcomeoutcome = v;UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final statefinishCompletion();}}

get

    public V get() throws InterruptedException, ExecutionException {int s = state;//如果是在执行中,则等待一会if (s <= COMPLETING)s = awaitDone(false, 0L);//返回结果return report(s);}/*** @throws CancellationException {@inheritDoc}*/public V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException {if (unit == null)throw new NullPointerException();//设置了超时时间,则等待一定的时间,如果还没有获取到返回异常int s = state;if (s <= COMPLETING &&(s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)throw new TimeoutException();return report(s);}private V report(int s) throws ExecutionException {Object x = outcome;//执行完成 返回x结果if (s == NORMAL)return (V)x;//如果任务取消,返回异常if (s >= CANCELLED)throw new CancellationException();throw new ExecutionException((Throwable)x);}

awaitDone

    private int awaitDone(boolean timed, long nanos)throws InterruptedException {final long deadline = timed ? System.nanoTime() + nanos : 0L;WaitNode q = null;boolean queued = false;for (;;) {//如果线程执行interrupted,直接抛出异常,并且将任务移除if (Thread.interrupted()) {removeWaiter(q);throw new InterruptedException();}int s = state;//状态大于COMPLETING 说明完成了if (s > COMPLETING) {if (q != null)q.thread = null;return s;}//else if (s == COMPLETING) // cannot time out yetThread.yield();else if (q == null)q = new WaitNode();else if (!queued)queued = UNSAFE.compareAndSwapObject(this, waitersOffset,q.next = waiters, q);else if (timed) {nanos = deadline - System.nanoTime();if (nanos <= 0L) {removeWaiter(q);return state;}LockSupport.parkNanos(this, nanos);}elseLockSupport.park(this);}}

小结

FutureTask是一个支持取消行为的异步任务执行器。该类实现了Future接口的方法。
如:

  1. 取消任务执行
  2. 查询任务是否执行完成
  3. 获取任务执行结果(”get“任务必须得执行完成才能获取结果,否则会阻塞直至任务完成)。

如果在当前线程中需要执行比较耗时的操作,但又不想阻塞当前线程时,可以把这些作业交给FutureTask,另开一个线程在后台完成,当当前线程将来需要时,就可以通过FutureTask对象获得后台作业的计算结果或者执行状态。

Future模式其实是多线程编程中常用的设计模式,主线程向另外一个线程提交任务,无需等待任务执行的结果,返回一个凭证,就是future,通过future.get()去获取结果。这个过程可能是阻塞的。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/196890.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ 设计模式 Forward Declaration Pimpl

放几轮跟 chatgpt 的对话&#xff0c;很精彩的回答 You 我有个问题&#xff0c;我的 main 目标依赖 src/gcp_subscriber.h 的 GCPSubscriber class 这个 class 有个 private 成员 google::cloud::pubsub::Subscriber 也就意味着我得在 gcp_subscriber.h 里面引用 google clou…

父类的@Autowired字段被继承后能否被注入

可以 示例 父类&#xff1a;Animal.class public class Animal {Autowiredprivate PrometheusAlertService prometheusAlertService;public void eat(){System.out.println("eat food");}} 子类&#xff1a;Dog.class Service public class Dog extends Animal …

马斯克没继续的工作,我帮他继续下去

还记得当初自己为什么选择计算机&#xff1f; 埃隆马斯克的第一份工作是在加拿大开始的。17岁时&#xff0c;他来到加拿大&#xff0c;但他的寻亲不遇&#xff0c;为了生存&#xff0c;他不得不打各种零工&#xff0c;包括在农场中种蔬菜和打扫粮仓&#xff0c;以及在木材厂锅…

电压驻波比

电压驻波比 关于IF端口的电压驻波比 一个信号变频后&#xff0c;从中频端口输出&#xff0c;它的输出跟输入是互异的。这个电压柱波比反映了它输出的能量有多少可以真正的输送到后端连接的器件或者设备。

python pyaudio 录取语音数据

python pyaudio 录取语音数据 pyaudio安装方法&#xff1a; pip install pyaudio如果这个不行&#xff0c;可以尝试&#xff1a; pip install pipwin pipwin install pyaudio代码如下&#xff1a; import pyaudio import waveRESPEAKER_RATE 44100 # 采样率&#xff0c;每…

JFrog----SBOM清单包含哪些:软件透明度的关键

文章目录 SBOM清单包含哪些&#xff1a;软件透明度的关键引言SBOM清单的重要性SBOM清单包含的核心内容SBOM的创建和管理结论 软件物料清单&#xff08;SBOM&#xff09;是一个在软件供应链安全中越来越重要的组成部分。它基本上是一份清单&#xff0c;详细列出了在特定软件产品…

从0开始使用Maven

文章目录 一.Maven的介绍即相关概念1.为什么使用Maven/Maven的作用2.Maven的坐标 二.Maven的安装三.IDEA编译器配置Maven环境1.在IDEA的单个工程中配置Maven环境2.方式2&#xff1a;配置Maven全局参数 四.IDEA编译器创建Maven项目五.IDEA中的Maven项目结构六.IDEA编译器导入Mav…

AI医疗交流平台【Docola】申请823万美元纳斯达克IPO上市

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 猛兽财经获悉&#xff0c;总部位于美国的AI医疗交流平台Docola近期已向美国证券交易委员会&#xff08;SEC&#xff09;提交招股书&#xff0c;申请在纳斯达克IPO上市&#xff0c;股票代码为 (DOCO) &#xff0c;Docola计划…

ARC120D Bracket Score 2

定义一个合法括号序列的权值为 ∑ ∣ a i − a j ∣ ∑∣a_i−a_j∣ ∑∣ai​−aj​∣&#xff0c;其中 ( i , j ) (i,j) (i,j) 满足第 i , j i,j i,j 位在括号序列中是配对的。 给定长度为 2 n 2n 2n 的序列 a a a&#xff0c;请求出长度为 2 n 2n 2n 的权值最大的合法…

RefCell 数据类型

内部可变性&#xff08;interior mutability&#xff09;是RUST的设计模式之一&#xff0c;它允许你在只持有不可变引用的前提下对数据进行修改。为了能改变数据&#xff0c;内部可变性模式在它的数据结构中使用了unsafe&#xff08;不安全&#xff09;代码来绕过RUST正常的可变…

开发工具idea中推荐插件

主要是记录一下idea中实用插件&#xff0c;方便开发&#xff0c;换个电脑工作的时候也可以直接在市场中下载使用。 1、Easy Javadoc 自动生成javadoc文档注释&#xff0c;基本上是按照字段名或者方法名翻译的&#xff0c;还是相当好用的。 2、EasyYapi 可以快捷生成接口文档…

Java中三种定时任务总结(schedule,quartz,xxl-job)

目录 1、Spring框架的定时任务 2、Quartz Quartz的用法 3、xxl-job 3.1 docker 安装xxl-job 3.2 xxl-job编程测试 补充&#xff1a;Java中自带的定时任务调度 1. java.util.Timer和java.util.TimerTask 2. java.util.concurrent.Executors和java.util.concurrent.Sche…

数据结构第六课 -----链式二叉树的实现

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…

centos7 设置静态ip

文章目录 设置VMware主机设置centos7 设置 设置VMware 主机设置 centos7 设置 vim /etc/sysconfig/network-scripts/ifcfg-ens33重启网络服务 service network restart检验配置是否成功 ifconfig ip addr

filter过滤器

package com.it.filter;import javax.servlet.*; import javax.servlet.annotation.WebFilter;import java.io.IOException;WebFilter(urlPatterns"/*") public class DemoFilter implements Filter {Override // 初始化的方法 只要调用一次public void init(Filte…

什么是深度「穿透式」供应链?苹果多层级穿透式供应链分析|徐礼昭

徐礼昭&#xff08;商派市场负责人&#xff0c;重构零售实验室负责人&#xff09; 什么是「穿透式供应链」&#xff1f; 穿透式供应链是一种新型的供应链体系&#xff0c;它强调纵深拓展和动态优化&#xff0c;以满足供应链的安全需求和价值需求。这种供应链体系由多个层级组成…

深圳市左下右上百度坐标

爬取百度POI的时候&#xff0c;别人的代码中有提到左下&#xff0c;右上坐标&#xff0c;但是没有说从哪里来&#xff0c;而且还是百度的坐标。 经纬度:左下角,右上角&#xff1a;113.529103,37.444122;115.486183,38.768031 墨卡托坐标:左下角,右上角&#xff1a;12638139.45,…

Python中的类(Class)和对象(Object)

目录 一、引言 二、类&#xff08;Class&#xff09; 1、类的定义 2、类的实例化 三、对象&#xff08;Object&#xff09; 1、对象的属性 2、对象的方法 四、类和对象的继承和多态性 1、继承 2、多态性 五、类与对象的封装性 1、封装的概念 2、Python中的封装实现…

个人博客搭建保姆级教程-HTML页面编写篇

选择模板 首先我们要选一个好的模板&#xff0c;然后对模板进行剪裁。我的模板是在站长之家进行下载的 素材下载-分享综合设计素材免费下载的平台-站长素材 我选的模板的具体地址是 个人博客资讯网页模板 这里需要我们学习一下在前边一篇文章里提到的HTML、JavaScript、CSS…

Learning Normal Dynamics in Videos with Meta Prototype Network 论文阅读

文章信息&#xff1a;发表在cvpr2021 原文链接&#xff1a; Learning Normal Dynamics in Videos with Meta Prototype Network 摘要1.介绍2.相关工作3.方法3.1. Dynamic Prototype Unit3.2. 视频异常检测的目标函数3.3. 少样本视频异常检测中的元学习 4.实验5.总结代码复现&a…