一、线程池的作用

  平时的业务中,如果要使用多线程,那么我们会在业务开始前创建线程,业务结束后,销毁线程。但是对于业务来说,线程的创建和销毁是与业务本身无关的,只关心线程所执行的任务。因此希望把尽可能多的cpu用在执行任务上面,而不是用在与业务无关的线程创建和销毁上面。而线程池则解决了这个问题。

  线程池的作用:线程池作用就是限制系统中执行线程的数量。根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果,从而避免平凡的创建和销毁线程带来的系统开销也有效的规避了因为创建的线程过多而耗尽系统资源导致服务器宕机。使用Runtime.getRuntime().availableProcessors();设置线程数量。

二、 java并发包提供的线程池 Executors类

  A、newFixedThreadPool  用来创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待

ExecutorService fixedThreadPool =Executors.newFixedThreadPool(1);

 public static ExecutorService newFixedThreadPool(int nThreads) {
//参数详解 第一个参数核心线程数,线程池在实例化的时候初始化时线程数
//第二个:该线程池最大线程数
//第三个:线程空闲时间(0L表示没有空闲时间即没有使用就会被回收)
//第四个:空闲时间单位
//第五个:LinkedBlockingQueue 无界队列 ,将没有线程处理的任务加入该队列中
return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
}

  B、newSingleThreadExecutor 用来创建一个单线程化的线程池,它只用唯一的工作线程来执行任务,一次只支持一个,所有任务按照指定的顺序执行

    ExecutorService fixedThreadPool = Executors.newSingleThreadExecutor();

public static ExecutorService newSingleThreadExecutor() {
//参数详解 第一个参数核心线程数,线程池在实例化的时候初始化时线程数
//第二个:该线程池最大线程数
//第三个:线程空闲时间(0L表示没有空闲时间即没有使用就会被回收)
//第四个:空闲时间单位
//第五个:LinkedBlockingQueue 无解队列 ,将没有线程处理的任务加入该队列中
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));
}

  C、newCachedThreadPool  用来创建一个可缓存线程池,该线程池没有长度限制,对于新的任务,如果有空闲的线程,则使用空闲的线程执行,如果没有,则新建一个线程来执行任务。如果线程池长度超过处理需要,可灵活回收空闲线程。

ExecutorService fixedThreadPool = Executors.newCachedThreadPool();

 public static ExecutorService newCachedThreadPool() {
//参数详解 第一个参数核心线程数,线程池在实例化的时候初始化时线程数
//第二个:Integer.MAX_VALUE 不限制该线程池的线程数
//第三个:线程空闲时间(60L 表示线程空闲60秒之后被回收)
//第四个:空闲时间单位 SECONDS 秒
//第五个:SynchronousQueue 无容量队列 ,将任务直接提交给线程处理自身不存储任务
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}

  D、newScheduledThreadPool 用来创建一个定长线程池,并且支持定时和周期性的执行任务

    ScheduledExecutorService executorsScheduled=Executors.newScheduledThreadPool(2);

public ScheduledThreadPoolExecutor(int corePoolSize) {
//参数详解 第一个参数核心线程数,线程池在实例化的时候初始化时线程数
//第二个:Integer.MAX_VALUE 不限制该线程池的线程数
//第三个:线程空闲时间(0 表示线程空闲0秒之后被回收)
//第四个:空闲时间单位 SECONDS 秒
//第五个:DelayedWorkQueue 延时队列
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue());
}

使用 newScheduledThreadPool实现定时器

package com.jalja.org.thread.executors;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class NewScheduledThreadPoolTest {
public static void main(String[] args) {
Runnable runnable=new ScheduledThread();
//实现定时器
ScheduledExecutorService executorsScheduled=Executors.newScheduledThreadPool(2);
//runnable 需要执行的任务 1:初始化时间(初始化延迟1秒后执行) 3:轮询时间(每隔3秒执行)
//TimeUnit.SECONDS:时间单位
ScheduledFuture<?> scheduledFuture= executorsScheduled.scheduleWithFixedDelay(runnable, 1,3, TimeUnit.SECONDS);
System.out.println("scheduledFuture:"+scheduledFuture);
}
}
class ScheduledThread implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName() +"=>开始");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +"=>结束");
}
}

 线程池中提交任务的两种方式:

execute()方法:该方法是 ExecutorService 接口的父类(接口)方法,该接口只有这一个方法。

public interface Executor {
void execute(Runnable command);
}

submit()方法:该方法是ExecutorService 接口的方法。

public interface ExecutorService extends Executor {
  ...
  <T> Future<T> submit(Callable<T> task);   <T> Future<T> submit(Runnable task, T result);   Future<?> submit(Runnable task);
  ...
}

从上面的源码以及讲解可以总结execute()和submit()方法的区别:

  1. 接收的参数不一样;

  2. submit()有返回值,而execute()没有;

三、自定义线程池

  在Java线程池中的newCachedThreadPool,newFixedThreadPool,newSingleThreadExecutor,newScheduledThreadPool这四个线程池在底层都是调用了ThreadPoolExecutor()这个构造方法。若Executors这个类无法满足我们的需求的时候,可以自己创建自定义的线程池。
ThreadPoolExecutor类的定义如下

                public ThreadPoolExecutor(int corePoolSize,//核心线程数--线程池初始化创建的线程数量
int maximumPoolSize,//最大线程数,线程池中能创建的最大线程数
long keepAliveTime,//线程存活时间
TimeUnit unit,//线程存货时间单位
BlockingQueue<Runnable> workQueue,//一个阻塞队列
ThreadFactory threadFactory//拒绝策略
) {……}

自定义线程池使用有界队列(ArrayBlockingQueue 、LinkedBlockingQueue )

  若有新的任务需要执行,如果线程池实际线程数小于corePoolSize核心线程数的时候,则优先创建线程。若大于corePoolSize时,则会将多余的线程存放在队列中,若队列已满,且最请求线程小于maximumPoolSize的情况下,则自定义的线程池会创建新的线程,若队列已满,且最请求线程大于maximumPoolSize的情况下,则执行拒绝策略,或其他自定义方式。

package com.jalja.org.thread.executors;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class ExecutorsTest {
public static void main(String[] args) {
ThreadPoolExecutor test=new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(2));
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.shutdown();
}
class ThreadTest implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

结果:

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.jalja.org.thread.executors.ExecutorsTest$ThreadTest@70dea4e rejected from java.util.concurrent.ThreadPoolExecutor@5c647e05[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at com.jalja.org.thread.executors.ExecutorsTest.main(ExecutorsTest.java:17)
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2

看结果可知有一个任务是没有执行直接抛出异常的。队列已满,且最请求线程大于maximumPoolSize的情况下,则执行拒绝策略,这里使用的是——AbortPolicy:直接抛出异常,系统正常工作(默认的策略)。

自定义线程池使用无界队列:

  对于无界队列除非系统资源耗尽,否则无界队列不存在任务入队失败的情况,若系统的线程数小于corePoolSize时,则新建线程执行corePoolSize,当达到corePoolSize后,则把多余的任务放入队列中等待执行若任务的创建和处理的速速差异很大,无界队列会保持快速增长,直到耗尽系统内存为之,对于无界队列的线程池maximumPoolSize并无真实用处。

四、拒绝策略

JDK提供策略:

1.AbortPolicy:直接抛出异常,系统正常工作。(默认的策略)

package com.jalja.org.thread.executors;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class ExecutorsTest {
public static void main(String[] args) {
BlockingQueue<Runnable> f=new LinkedBlockingQueue<Runnable>(2);
ThreadPoolExecutor test=new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS,f);
try {
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
} catch (RejectedExecutionException e) {
e.printStackTrace();
System.out.println("超过有界队列的数据记录日志");
}
test.shutdown();
}
class ThreadTest implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

2.CallerRunsPolicy:只要线程池未关闭,该策略直接在调用者线程中执行,运行当前被丢弃的任务。
3.DiscardOrderstPolicy:丢弃最老的请求,尝试再次提交当前任务。
4.丢弃无法处理的任务,不给于任何处理。

自定义策略:需要实现RejectedExecutionHandler接口

package com.jalja.org.thread.executors;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class ExecutorsTest {
public static void main(String[] args) {
BlockingQueue<Runnable> f=new LinkedBlockingQueue<Runnable>(2);
ThreadPoolExecutor test=new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS,f, new MyRejected());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.execute(new ExecutorsTest().new ThreadTest());
test.shutdown();
}
class ThreadTest implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyRejected implements RejectedExecutionHandler{
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println("执行异常的任务加入日志");
}
}

java 线程之executors线程池的更多相关文章

  1. java多线程之 Executors线程池管理

    1. 类 Executors 此类中提供的一些方法有: 1.1 public static ExecutorService newCachedThreadPool() 创建一个可根据需要创建新线程的线 ...

  2. Java多线程-新特性-线程池

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...

  3. 三、VIP课程:并发编程专题->01-并发编程之Executor线程池详解

    01-并发编程之Executor线程池详解 线程:什么是线程&多线程 线程:线程是进程的一个实体,是 CPU 调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系 ...

  4. Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  5. Java多线程系列--“JUC线程池”06之 Callable和Future

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

  6. Java多线程系列--“JUC线程池”03之 线程池原理(二)

    概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...

  7. JAVA基础拾遗-论线程池的线程粒度划分与深浅放置

    摘要:多线程任务处理对提高性能很有帮助,在Java中提供的线程池也方便了对多线程任务的实现.使用它很简单,而如果进行了不正确的使用,那么代码将陷入一团乱麻.因此如何正确地使用它,如以下分享,这个技能你 ...

  8. Java并发编程:线程池的使用(转)

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  9. Java视频扩展知识 线程池的了解

     Java视频扩展知识   线程池的了解 1.简单介绍: Jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用.为我们在开发中处理线程的 ...

随机推荐

  1. 第45篇 js操作打开本地程序

    原文地址:http://blog.laofu.online/2017/06/10/how-js-controlApp/ 背景 假设有这样一个产品,一个web和一个winform客户端,在客户在web的 ...

  2. 踩一踩微信小程序开发的坑---tabBar

    最近忙于开发视频直播的项目,小程序学习也放置了两三个星期了,web开发者工具更新到新版,发现上个版本做的demo不显示了

  3. 织梦dedecms单标签、双标签

    标签是dedecms的核心,dedecms的标签也跟html标签一样,同样分单标签和双标签. 我不会讲单标签有那些,双标签有那些,也不会叫大家去背那些是单标签,那些是双标签.如果去背这些标签,这样学起 ...

  4. jQuery时间日期插件laydate,兼容bootstrap

    解压后,将laydate整个文件放至您项目的任意目录,不要移动其文件结构,它们具有完整的依赖体系. 使用只需在页面引入laydate.js即可. 如果您的网站的js采用合并或模块加载,您需要打开lay ...

  5. 一些java方面面试题,没事做做看看(带答案)

    1. Switch能否用string做参数? a.在?Java? <http://lib.csdn.net/base/java>7 之前, switch 只能支持byte,short,ch ...

  6. ajax请求返回数据,模板中的数据处理

    /*ajax请求返回数据,模板中的数据处理*/ function QueryGameAsset(){ var new_start_time=$('#new_start_time').val();//开 ...

  7. Iconfont 矢量图标库的应用

    前言: 在项目开发中,不免在标签栏,工具栏等应用各种各样的小图标. 然后老旧的做法就是要UI设计出各种图标并生成图片给到我们,但是这样就存在了一个问题,每次请求页面的时候就需要发送请求请求图片,这样不 ...

  8. Pycharm创建的virtualenv环境缺失pip.exe的问题(Windows系统)

    Windows环境: 1. Python安装在d:\Python\Python35下, Python新版本安装时默认会勾选pip功能 2. PyCharm的Settings中Create Virtua ...

  9. php打包文件为ZIP包后下载到本地

    这是一个工作中需要打包下载当前产品的所有图片到本地,文件格式为ZIP压缩包,打包下载文件跟图片一样,本程序细节为实际情况,使用需按照自己实际情况书写:<?php/**************** ...

  10. centos+apache 2.x 开启gzip压缩

    最近做了一个网站(PHP+Apache+MySQL),挂在百度云平台上面,基本配置是2G内存+5Mb带宽,每次打开主页都需要2-3s左右的时间,对于一个垂直搜索引擎来说,用户体验肯定会很差. 于是开始 ...