executor

原理剖析(第 003 篇)ThreadPoolExecutor工作原理分析

瘦欲@ 提交于 2019-12-17 19:48:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 原理剖析(第 003 篇)ThreadPoolExecutor工作原理分析 一、大致介绍 1、相信大家都用过线程池,对该类ThreadPoolExecutor应该一点都不陌生了; 2、我们之所以要用到线程池,线程池主要用来解决线程生命周期开销问题和资源不足问题; 3、我们通过对多个任务重用线程以及控制线程池的数目可以有效防止资源不足的情况; 4、本章节就着重和大家分享分析一下JDK8的ThreadPoolExecutor核心类,看看线程池是如何工作的; 二、基本字段方法介绍 2.1 构造器 1、四个构造器: // 构造器一 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } // 构造器二 public ThreadPoolExecutor(int

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

故事扮演 提交于 2019-12-17 17:04:31
总览 开篇来一些废话。下图是 java 线程池几个相关类的继承结构: 先简单说说这个继承结构,Executor 位于最顶层,也是最简单的,就一个 execute(Runnable runnable) 接口方法定义。 ExecutorService 也是接口,在 Executor 接口的基础上添加了很多的接口方法,所以 一般来说我们会使用这个接口 。 然后再下来一层是 AbstractExecutorService,从名字我们就知道,这是抽象类,这里实现了非常有用的一些方法供子类直接使用,之后我们再细说。 然后才到我们的重点部分 ThreadPoolExecutor 类,这个类提供了关于线程池所需的非常丰富的功能。 另外,我们还涉及到下图中的这些类: 同在并发包中的 Executors 类,类名中带字母 s,我们猜到这个是工具类,里面的方法都是静态方法,如以下我们最常用的用于生成 ThreadPoolExecutor 的实例的一些方法: public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } public

How jobs are assigned to executors in Spark Streaming?

孤者浪人 提交于 2019-12-17 16:33:11
问题 Let's say I've got 2 or more executors in a Spark Streaming application. I've set the batch time of 10 seconds, so a job is started every 10 seconds reading input from my HDFS. If the every job lasts for more than 10 seconds, the new job that is started is assigned to a free executor right? Even if the previous one didn't finish? I know it seems like a obvious answer but I haven't found anything about job scheduling in the website or on the paper related to Spark Streaming. If you know some

asyncio: Is it possible to cancel a future been run by an Executor?

会有一股神秘感。 提交于 2019-12-17 07:39:37
问题 I would like to start a blocking function in an Executor using the asyncio call loop.run_in_executor and then cancel it later, but that doesn't seem to be working for me. Here is the code: import asyncio import time from concurrent.futures import ThreadPoolExecutor def blocking_func(seconds_to_block): for i in range(seconds_to_block): print('blocking {}/{}'.format(i, seconds_to_block)) time.sleep(1) print('done blocking {}'.format(seconds_to_block)) @asyncio.coroutine def non_blocking_func

Any good Spring threading with a TaskExecutor examples? [closed]

孤人 提交于 2019-12-17 07:13:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm trying to get a handle on how to implement threading in a Java application that uses Spring for transaction management. I've found the TaskExecutor section in the Spring documentation, and ThreadPoolTaskExecutor looks like it would fit my needs; ThreadPoolTaskExecutor This implementation can only be used in

18-NioEventLoop实例化过程

谁都会走 提交于 2019-12-16 22:24:33
文章目录 NioEventLoop实例化过程 一、构造方法 1.1 NioEventLoop 1.2 SingleThreadEventLoop 1.3 SingleThreadEventExecutor 1.4 AbstractEventExecutor 二、EventLoop 与 Channel 的关联 2.1 Channel注册到EventLoop 2.2 EventLoopGroup注册Channel 三、EventLoop启动 3.1 SingleThreadEventExecutor#startThread 3.2 NioEventLoop#run 四、小结 NioEventLoop实例化过程 本文集合代码流程来分析 NioEventLoop 的实例化过程 开篇我们牢记 NioEventLoop 的两个功能: 1.处理IO事件;执行与 Channel 相关的 IO 操作, 包括调用 select 等待就绪的 IO 事件、读写数据与数 据的处理等; 2.执行提交的任务;作为任务队列, 执行 taskQueue 中的任务, 例如用户调用 eventLoop.schedule 提 交的定时任务也是这个线程执行的; 一、构造方法 实例化自然离不开构造方法,不管是我们直接构造对象还是框架内部去构造对象都会走构造方法,因为NioEventLoop 是一个最底层的实现类

spark shuffle详解(hashShuffle和sortShuffle)

依然范特西╮ 提交于 2019-12-16 19:12:00
Shuffle简介 Shuffle描述着数据从map task输出到reduce task输入的这段过程。shuffle是连接Map和Reduce之间的桥梁,Map的输出要用到Reduce中必须经过shuffle这个环节,shuffle的性能高低直接影响了整个程序的性能和吞吐量。因为在分布式情况下,reduce task需要跨节点去拉取其它节点上的map task结果。这一过程将会产生网络资源消耗和内存,磁盘IO的消耗。通常shuffle分为两部分:Map阶段的数据准备和Reduce阶段的数据拷贝处理。一般将在map端的Shuffle称之为Shuffle Write,在Reduce端的Shuffle称之为Shuffle Read. Hadoop MapReduce Shuffle Apache Spark 的 Shuffle 过程与 Apache Hadoop 的 Shuffle 过程有着诸多类似,一些概念可直接套用,例如,Shuffle 过程中,提供数据的一端,被称作 Map 端,Map 端每个生成数据的任务称为 Mapper,对应的,接收数据的一端,被称作 Reduce 端,Reduce 端每个拉取数据的任务称为 Reducer,Shuffle 过程本质上都是将 Map 端获得的数据使用分区器进行划分,并将数据发送给对应的 Reducer 的过程。

Mybatis-运行原理

≯℡__Kan透↙ 提交于 2019-12-16 19:03:41
构建 SqlFactory 过程: SqlFactory 的核心功能就是提供创建 MyBatis 的核心接口 SqlSession. SqlSession 采用建造者模式进行构建: 首先通过 org.apache.ibatis.builder.xml.XMLConfigBuilder 解析配置的 XML 文件, 读出所配置的参数, 并将读取的内容存入 org.apache.ibatis.session.Configuration 类对象中. Configuration 对象采用的是单例模式. 通过 Configuration 对象去创建 SqlSessionFactory. SqlSessionFactory 是一个接口, MyBatis 提供了一个默认的实现类 org.apache.ibatis.session.defaults.DefaultSqlSessionFactory. private void parseConfiguration ( XNode root ) { try { propertiesElement ( root . evalNode ( "properties" ) ) ; Properties settings = settingsAsProperties ( root . evalNode ( "settings" ) ) ;

Spring异步任务处理,@Async的配置和使用

与世无争的帅哥 提交于 2019-12-16 15:59:21
这个注解用于标注某个方法或某个类里面的所有方法都是需要异步处理的。被注解的方法被调用的时候,会在新线程中执行,而调用它的方法会在原来的线程中执行。这样可以避免阻塞、以及保证任务的实时性。适用于处理log、发送邮件、短信……等。 注解的应用范围: 类:表示这个类中的所有方法都是异步的 方法:表示这个方法是异步的,如果类也注解了,则以这个方法的注解为准 相关的配置: <task:annotation-driven />配置: executor:指定一个缺省的executor给@Async使用。 例子: <task:annotation-driven executor="asyncExecutor" /> <task:executor />配置参数: id:当配置多个executor时,被@Async("id")指定使用;也被作为线程名的前缀。 pool-size: core size:最小的线程数,缺省:1 max size:最大的线程数,缺省:Integer.MAX_VALUE queue-capacity:当最小的线程数已经被占用满后,新的任务会被放进queue里面,当这个queue的capacity也被占满之后,pool里面会创建新线程处理这个任务,直到总线程数达到了max size,这时系统会拒绝这个任务并抛出TaskRejectedException异常(缺省配置的情况下

netty之NioEventLoopGroup和NioEventLoop

早过忘川 提交于 2019-12-15 22:40:52
Netty实战抠的图,说明了Channel、EventLoop、Thread以及EventLoopGroup之间的关系。 一个EventLoopGroup包含一个或多个EventLoop 一个EventLoop在他的生命周期内之和一个Thread绑定 所以由EventLoop处理的I/O事件都将在他专有的Thread上被处理 一个Channel在他的生命周期内只注册于一个EventLoop 一个EventLoop可能会被分配给一个或多个Channel EventLoop只定义了一个parent()方法,用于返回到当前EventLoop实现的实例所属的EventLoopGroup的引用 public interface EventLoop extends OrderedEventExecutor , EventLoopGroup { EventLoopGroup parent ( ) ; } parent()方法重写了EventExecutor的EventExecutorGroup .parent()方法 public interface EventExecutor extends EventExecutorGroup { EventExecutor next ( ) ; EventExecutorGroup parent ( ) ; . . . . . }