多线程(七)JDK原生线程池
如同数据库连接一样,线程的创建、切换和销毁同样会耗费大量的系统资源。为了复用创建好的线程,减少频繁创建线程的次数,提高线程利用率可以引用线程池技术。使用线程池的优势有如下几点:
1、保持一定数量的线程,减少了线程频繁创建和销毁资源消耗。
2、使用线程的时候直接由线程池中取出线程,省去了创建线程的时间,侧面提高了系统的响应时间。
3、需要使用线程的时候直接从线程池中取出,避免了人为的不合理创建线程,减少了潜在的风险。
Doug Lea在实现JUC中提供了原生的线程池,并提供了各种线程管理策略来适应于不同的使用场景。使用的时候可通过Executors获取各种线程池实例。

这里提供了六对12个方法来创建ExecutorService,其中每种类型的ExecutorService可以适用于不同的应用场景,对线程的管理策略也各不相同。下面就看一下各个方法的注释:
/**
* Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
* 创建一个线程池,需要的时候会创建新的线程,如果有可用的线程则会复用以前已经创建好的线程。
* These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks.
* 这些线程池通常情况下可以提升哪些短期异步任务的性能
* Calls to {@code execute} will reuse previously constructed threads if available.
* 如果以创建的线程状态可用的话,调用execute可以复用他们
* If no existing thread is available, a new thread will be created and added to the pool.
* 如果不存在可用状态的线程,那么将会创建一个新线程同时会把该线程添加到线程池中
* Threads that have not been used for sixty seconds are terminated and removed from the cache.
*那些超过60s没用的线程将会被销毁同时从缓存中移除
* Thus, a pool that remains idle for long enough will not consume any resources.
*因此长时间空闲的线程池不会消耗任何资源
* Note that pools with similar properties but different details (for example, timeout parameters) may be created using {@link ThreadPoolExecutor} constructors.
*可以使用ThreadPoolExecutor创建性质相似但实现细节不同的线程池
* @return the newly created thread pool
*/ public static ExecutorService newCachedThreadPool(); //可以使用自定义的ThreadFactory 类创建线程,其它和无参方法一致
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory);
/**
* Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.
*创建一个可重用、固定数量线程的线程池
* At any point, at most {@code nThreads} threads will be active processing tasks.
*任何时间最多只有 nThreads 个线程被激活来执行任务
* If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.
* 当无可用空闲线程的时候,如果有新任务被提交,这些新任务将会一直等待直至有可用线程来执行。
* If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.
*如果任何线程正常关闭之前在执行过程中因失败而提前终止,那么如果有未被执行的后续任务,则会创建新的线程来继续执行。
* The threads in the pool will exist until it is explicitly {@link ExecutorService#shutdown shutdown}.
* 线程池中的所有线程在明确掉用shutdown之后将会退出
*
* @param nThreads the number of threads in the pool
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code nThreads <= 0}
*/
public static ExecutorService newFixedThreadPool(int nThreads);
//可自定义ThreadFactory
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory);
/**
* Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
* 创建一个线程池,该线程在延迟指定时间之后可以周期性的执行线程体
* @param corePoolSize the number of threads to keep in the pool,
* even if they are idle
* @return a newly created scheduled thread pool 注意返回值类型是ScheduledExecutorService,不要使用ExecutorService来接收,否则找不到schedule执行方法
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize);
//可自定义ThreadFactory
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory);
/**
* Creates an Executor that uses a single worker thread operating off an unbounded queue.
*创建一个Executor,使用一个线程来工作,该线程存储在LinkedBlockingQueue中
* (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.)
*注,如果任何线程正常关闭之前在执行过程中因失败而提前终止,那么如果有未被执行的后续任务,则会创建新的线程来继续执行。
* Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.
* 任务是按顺序执行的,任何时间都只有一个线程来执行任务
* Unlike the otherwise equivalent {@code newFixedThreadPool(1)} the returned executor is guaranteed not to be reconfigurable to use additional threads.
*
* @return the newly created single-threaded Executor
*/ public static ExecutorService newSingleThreadExecutor();
//可自定义ThreadFactory
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory);
/**
* Creates a single-threaded executor that can schedule commands
* to run after a given delay, or to execute periodically.
* (Note however that if this single
* thread terminates due to a failure during execution prior to
* shutdown, a new one will take its place if needed to execute
* subsequent tasks.) Tasks are guaranteed to execute
* sequentially, and no more than one task will be active at any
* given time. Unlike the otherwise equivalent
* {@code newScheduledThreadPool(1)} the returned executor is
* guaranteed not to be reconfigurable to use additional threads.
* @return the newly created scheduled executor
*/ public static ScheduledExecutorService newSingleThreadScheduledExecutor(); public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)
使用示例:
package thread.blogs.threadpool; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; /**
* Created by PerkinsZhu on 2017/8/31 13:59.
*/
public class PoolTest { public static void main(String[] args) {
// testCachedThreadPool();
// testSingleThreadExecutor();
// testFixedThreadPool();
testScheduledThreadPool();
}
private static ThreadFactory myFactory = new MyThreadFactory(); private static void testSingleThreadExecutor() {
//一个一个的依次执行
doHandle(Executors.newSingleThreadExecutor(myFactory));
} private static void testFixedThreadPool() {
//两个两个的一起执行
doHandle(Executors.newFixedThreadPool(2,myFactory));
} private static void testCachedThreadPool() {
//10个一起一次性执行完
doHandle(Executors.newCachedThreadPool(myFactory));
} private static void testScheduledThreadPool() {
//定时周期执行
Executors.newScheduledThreadPool(1,myFactory).scheduleAtFixedRate(runnable, 500, 2000, TimeUnit.MILLISECONDS);
} private static Runnable runnable = () -> {
sleep(1000);
System.out.println(Thread.currentThread().getName() + " work!!!");
}; private static void doHandle(ExecutorService executorService) {
for (int i = 0; i < 10; i++) {
executorService.execute(runnable);
}
executorService.shutdown();
} private static void sleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} class MyThreadFactory implements ThreadFactory {//自定义ThreadFactory
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix; MyThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "☆☆☆--" + poolNumber.getAndIncrement() + "-****-";
} public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
} }
在使用的时候必须明白各种线程池是否适用于自己的情景,选取合适的线程池进行使用。
=========================================
原文链接:多线程(七)JDK原生线程池转载请注明出处!
=========================================
---end
多线程(七)JDK原生线程池的更多相关文章
- 原生线程池这么强大,Tomcat 为何还需扩展线程池?
前言 Tomcat/Jetty 是目前比较流行的 Web 容器,两者接受请求之后都会转交给线程池处理,这样可以有效提高处理的能力与并发度.JDK 提高完整线程池实现,但是 Tomcat/Jetty 都 ...
- java多线程系列六、线程池
一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池. 2. 使用线程池的好处 a) 降低资源的消耗.使用线程池不用频繁的创建线程和销毁线程 b) 提高响应速度,任 ...
- java核心-多线程(6)-线程池-ThreadPoolExecutor
1.java多线程编程少不了使用线程池,线程池相关的工具类所在jdk包,java.util.concurrent 2.使用示例 demo1 public class ThreadPoolDemo { ...
- C#多线程之旅(3)——线程池
v博客前言 先交代下背景,写<C#多线程之旅>这个系列文章主要是因为以下几个原因:1.多线程在C/S和B/S架构中用得是非常多的;2.而且多线程的使用是非常复杂的,如果没有用好,容易造成很 ...
- C# 多线程的自动管理(线程池) 基于Task的方式
C# 多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况: 1. 应用程序中线程把大部分的时间花费在等待状态,等待某个事件发生,然后给予响应.这一般使用 ThreadPool(线程 ...
- 第十章:Python高级编程-多线程、多进程和线程池编程
第十章:Python高级编程-多线程.多进程和线程池编程 Python3高级核心技术97讲 笔记 目录 第十章:Python高级编程-多线程.多进程和线程池编程 10.1 Python中的GIL 10 ...
- java多线程总结五:线程池的原理及实现
1.线程池简介: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创 ...
- Java多线程并发04——合理使用线程池
在此之前,我们已经了解了关于线程的基本知识,今天将为各位带来,线程池这一技术.关注我的公众号「Java面典」了解更多 Java 相关知识点. 为什么使用线程池?线程池做的工作主要是控制运行的线程的数量 ...
- java多线程详解(7)-线程池的使用
在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, 这样频繁创建线程就会大大降低系 ...
随机推荐
- 机器学习 —— 基础整理(五)线性回归;二项Logistic回归;Softmax回归及其梯度推导;广义线性模型
本文简单整理了以下内容: (一)线性回归 (二)二分类:二项Logistic回归 (三)多分类:Softmax回归 (四)广义线性模型 闲话:二项Logistic回归是我去年入门机器学习时学的第一个模 ...
- (转)JAVA排序汇总
JAVA排序汇总 package com.softeem.jbs.lesson4; import java.util.Random; /** * 排序测试类 * * 排序算法的分类如下: * 1.插入 ...
- Maven “Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create...”问题总结
今天学习Maven的过程中,一直遇到一个问题:用maven指令构建新项目时,一直报错,用的 Maven 3.2 , JDK 6. 构建的命令: 错误信息: 解决方案: 在StackOverFlow上找 ...
- ETL作业调度软件TASKCTL4.1集群部署
熟悉TASKCTL4.1一段时间后,觉得它的调度逻辑什么的都还不错,但是感觉单机部署不太够用.想实现跨机调度作业,就要会TASKCTL的集群部署.下面就是我在网上找到的相关资料,非原创. 单机部署成功 ...
- 移动端300ms点击事件的延迟
移动端click事件300ms延迟 移动端click事件300ms的延迟在目前看来,已经是老生常谈了. 以下内容,我会在参考资源的基础上谈谈我对移动端click事件300ms延迟的一些理解.本人愚昧, ...
- expdp导出文件,ORA-01555: 快照过旧: 回退段号 716
快照号过旧,回退段号过小,信息如下:ORA-31693: 表数据对象 "CZBSDB"."SMS_RESULT_RECORD" 无法加载/卸载并且被跳过, 错误 ...
- ORA-01036: 非法的变量名/编号 解决方案
今天又一次遇到了 ORA-01036: 非法的变量名/编号 的问题,之前在项目中也遇见过这个问题,但是具体怎么解决的忘记了,今天又是遇见了,花了半个小时才解决.我今天遇到的情况是这样的: 存储过程中有 ...
- Oracle sql 查询结果某一列字段合并成为一条数据
使用oracle中自带函数 wmsys.wm_concat(需合并列的字段名) 用法如下: select code,name,wmsys.wm_concat(baname) from tab gro ...
- Fancytree Javascript Tree TreeTable 树介绍和使用
Fancytree是一个非常棒的Javascript控件,功能强大,文档健全.在做Javascript Tree控件选型时,主要基于以下几点选择了Fancytree 在Javascript Tree控 ...
- 用Python处理实验数据
开篇语 近来忙于考试以及应付专业课,基本很少写简书了.昨晚攻坚了三个学生工作的任务(妈妈的吻.好久没有这么疯狂工作了.还是很爽的哦!) 今天难得清静,虽然上课还是沉浸于完成任务的放纵式玩手机中,但是也 ...