Java thread pool manages the pool of worker threads. It contains a queue that keeps tasks waiting to get executed. We can use ThreadPoolExecutor to create thread pool in Java.

Java线程池管理工作线程池。 它包含一个使任务等待执行的队列。 我们可以使用ThreadPoolExecutor在Java中创建线程池。

Java thread pool manages the collection of Runnable threads. The worker threads execute Runnable threads from the queue. java.util.concurrent.Executors provide factory and support methods for java.util.concurrent.Executor interface to create the thread pool in java.

Java线程池管理可运行线程的集合。 工作线程从队列中执行可运行线程。 java.util.concurrent.Executorsjava.util.concurrent.Executor接口提供工厂和支持方法,以在Java中创建线程池。

Executors is a utility class that also provides useful methods to work with ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes through various factory methods.

Executors是一个实用程序类,它还提供了有用的方法,可以通过各种工厂方法与ExecutorService,ScheduledExecutorService,ThreadFactory和Callable类一起使用。

Let’s write a simple program to explain it’s working.

让我们编写一个简单的程序来说明它的工作原理。

First, we need to have a Runnable class, named WorkerThread.java

首先,我们需要有一个名为WorkerThread.java的Runnable类。

package com.journaldev.threadpool;public class WorkerThread implements Runnable {private String command;public WorkerThread(String s){this.command=s;}@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);processCommand();System.out.println(Thread.currentThread().getName()+" End.");}private void processCommand() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}@Overridepublic String toString(){return this.command;}
}

ExecutorService示例 (ExecutorService Example)

Here is the test program class SimpleThreadPool.java, where we are creating fixed thread pool from Executors framework.

这是测试程序类SimpleThreadPool.java ,在这里我们从Executors框架创建固定线程池。

package com.journaldev.threadpool;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class SimpleThreadPool {public static void main(String[] args) {ExecutorService executor = Executors.newFixedThreadPool(5);for (int i = 0; i < 10; i++) {Runnable worker = new WorkerThread("" + i);executor.execute(worker);}executor.shutdown();while (!executor.isTerminated()) {}System.out.println("Finished all threads");}
}

In the above program, we are creating a fixed-size thread pool of 5 worker threads. Then we are submitting 10 jobs to this pool, since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state, as soon as one of the job is finished, another job from the wait queue will be picked up by worker thread and get’s executed.

在上面的程序中,我们正在创建一个由5个工作线程组成的固定大小的线程池。 然后,我们将10个作业提交到该池中,因为该池的大小为5,它将开始处理5个作业,其他作业将处于等待状态,一旦其中一个作业完成,来自等待队列的另一个作业将被工作线程拾取并执行。

Here is the output of the above program.

这是上面程序的输出。

pool-1-thread-2 Start. Command = 1
pool-1-thread-4 Start. Command = 3
pool-1-thread-1 Start. Command = 0
pool-1-thread-3 Start. Command = 2
pool-1-thread-5 Start. Command = 4
pool-1-thread-4 End.
pool-1-thread-5 End.
pool-1-thread-1 End.
pool-1-thread-3 End.
pool-1-thread-3 Start. Command = 8
pool-1-thread-2 End.
pool-1-thread-2 Start. Command = 9
pool-1-thread-1 Start. Command = 7
pool-1-thread-5 Start. Command = 6
pool-1-thread-4 Start. Command = 5
pool-1-thread-2 End.
pool-1-thread-4 End.
pool-1-thread-3 End.
pool-1-thread-5 End.
pool-1-thread-1 End.
Finished all threads

The output confirms that there are five threads in the pool named from “pool-1-thread-1” to “pool-1-thread-5” and they are responsible to execute the submitted tasks to the pool.

输出确认池中有五个线程从“ pool-1-thread-1”到“ pool-1-thread-5”命名,它们负责执行向池中提交的任务。

ThreadPoolExecutor示例 (ThreadPoolExecutor Example)

Executors class provide simple implementation of ExecutorService using ThreadPoolExecutor but ThreadPoolExecutor provides much more feature than that. We can specify the number of threads that will be alive when we create ThreadPoolExecutor instance and we can limit the size of thread pool and create our own RejectedExecutionHandler implementation to handle the jobs that can’t fit in the worker queue.

Executors类提供简单实现的ExecutorService的使用的ThreadPoolExecutor但ThreadPoolExecutor的提供了更多的功能不止于此。 我们可以指定创建ThreadPoolExecutor实例时仍处于活动状态的线程数,并且可以限制线程池的大小并创建自己的RejectedExecutionHandler实现,以处理无法容纳在工作队列中的作业。

Here is our custom implementation of RejectedExecutionHandler interface.

这是我们对RejectedExecutionHandler接口的自定义实现。

package com.journaldev.threadpool;import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {@Overridepublic void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {System.out.println(r.toString() + " is rejected");}}

ThreadPoolExecutor provides several methods using which we can find out the current state of the executor, pool size, active thread count and task count. So I have a monitor thread that will print the executor information at a certain time interval.

ThreadPoolExecutor提供了几种方法,通过这些方法我们可以找到执行器的当前状态,池大小,活动线程数和任务数。 因此,我有一个监视线程,该线程将在特定时间间隔打印执行程序信息。

package com.journaldev.threadpool;import java.util.concurrent.ThreadPoolExecutor;public class MyMonitorThread implements Runnable
{private ThreadPoolExecutor executor;private int seconds;private boolean run=true;public MyMonitorThread(ThreadPoolExecutor executor, int delay){this.executor = executor;this.seconds=delay;}public void shutdown(){this.run=false;}@Overridepublic void run(){while(run){System.out.println(String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",this.executor.getPoolSize(),this.executor.getCorePoolSize(),this.executor.getActiveCount(),this.executor.getCompletedTaskCount(),this.executor.getTaskCount(),this.executor.isShutdown(),this.executor.isTerminated()));try {Thread.sleep(seconds*1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}

Here is the thread pool implementation example using ThreadPoolExecutor.

这是使用ThreadPoolExecutor的线程池实现示例。

package com.journaldev.threadpool;import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class WorkerPool {public static void main(String args[]) throws InterruptedException{//RejectedExecutionHandler implementationRejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();//Get the ThreadFactory implementation to useThreadFactory threadFactory = Executors.defaultThreadFactory();//creating the ThreadPoolExecutorThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory, rejectionHandler);//start the monitoring threadMyMonitorThread monitor = new MyMonitorThread(executorPool, 3);Thread monitorThread = new Thread(monitor);monitorThread.start();//submit work to the thread poolfor(int i=0; i<10; i++){executorPool.execute(new WorkerThread("cmd"+i));}Thread.sleep(30000);//shut down the poolexecutorPool.shutdown();//shut down the monitor threadThread.sleep(5000);monitor.shutdown();}
}

Notice that while initializing the ThreadPoolExecutor, we are keeping initial pool size as 2, maximum pool size to 4 and work queue size as 2. So if there are 4 running tasks and more tasks are submitted, the work queue will hold only 2 of them and the rest of them will be handled by RejectedExecutionHandlerImpl.

请注意,在初始化ThreadPoolExecutor时,我们将初始池大小保持为2,最大池大小为4,工作队列大小为2。因此,如果有4个正在运行的任务并且提交了更多任务,则工作队列将仅容纳其中2个其余的将由RejectedExecutionHandlerImpl处理。

Here is the output of the above program that confirms the above statement.

这是上述程序的输出,确认了上述声明。

pool-1-thread-1 Start. Command = cmd0
pool-1-thread-4 Start. Command = cmd5
cmd6 is rejected
pool-1-thread-3 Start. Command = cmd4
pool-1-thread-2 Start. Command = cmd1
cmd7 is rejected
cmd8 is rejected
cmd9 is rejected
[monitor] [0/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-4 End.
pool-1-thread-1 End.
pool-1-thread-2 End.
pool-1-thread-3 End.
pool-1-thread-1 Start. Command = cmd3
pool-1-thread-4 Start. Command = cmd2
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-4 End.
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true

Notice the change in active, completed and total completed task count of the executor. We can invoke shutdown() method to finish execution of all the submitted tasks and terminate the thread pool.

注意执行程序的活动,已完成和已完成任务总数的变化。 我们可以调用shutdown()方法来完成所有提交的任务的执行并终止线程池。

If you want to schedule a task to run with delay or periodically then you can use ScheduledThreadPoolExecutor class. Read more about them at Java Schedule Thread Pool Executor.

如果要安排任务延迟或定期运行,则可以使用ScheduledThreadPoolExecutor类。 在Java Schedule Thread Pool Executor中了解有关它们的更多信息。

翻译自: https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-executorservice

ThreadPoolExecutor – Java线程池示例相关推荐

  1. 使用执行程序和ThreadPoolExecutor的Java线程池示例

    线程池管理工作线程池,它包含一个队列,使任务等待执行. 线程池管理可运行线程的集合,工作线程从队列中执行可运行线程. java.util.concurrent.Executors提供java.util ...

  2. java线程池示例_Java线程连接示例

    java线程池示例 Java Thread join method can be used to pause the current thread execution until unless the ...

  3. Java线程池示例:任务窃取线程池WorkStealingPool

  4. Java线程池示例:并行计算200000以内的质数个数

    static void countTime() throws Exception {long start = System.currentTimeMillis();List<Integer> ...

  5. java线程池_Java 并发编程 线程池源码实战

    作者 | 马启航 杏仁后端工程师.「我头发还多,你们呢?」 一.概述 笔者在网上看了好多的关于线程池原理.源码分析相关的文章,但是说实话,没有一篇让我觉得读完之后豁然开朗,完完全全的明白线程池,要么写 ...

  6. java 线程池 简介

    线程的实现方式 java 多线程之 extends Thread java 多线程之 implements Runnable java 多线程之 implements Callable 线程池的使用 ...

  7. java线程池使用详解ThreadPoolExecutor使用示例

    一 使用线程池的好处 二 Executor 框架 2.1 简介 2.2 Executor 框架结构(主要由三大部分组成) 1) 任务(Runnable /Callable) 2) 任务的执行(Exec ...

  8. JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue .

    2019独角兽企业重金招聘Python工程师标准>>> 从Java5开始,Java提供了自己的线程池.每次只执行指定数量的线程,java.util.concurrent.Thread ...

  9. 【Java 并发编程】线程池机制 ( 线程池阻塞队列 | 线程池拒绝策略 | 使用 ThreadPoolExecutor 自定义线程池参数 )

    文章目录 一.线程池阻塞队列 二.拒绝策略 三.使用 ThreadPoolExecutor 自定义线程池参数 一.线程池阻塞队列 线程池阻塞队列是线程池创建的第 555 个参数 : BlockingQ ...

最新文章

  1. 基因结构图绘制-单个基因
  2. Oracle VM VirtualBox上安装windows server2008R2做SharePointServer2010开发(中)
  3. VTK:可视化之StreamLines
  4. [CQOI2015]任务查询系统
  5. 用虚拟机安装了红帽后,我确实没设置用户名密码,但现在一打开就让输入用户名密码。这种情况该怎么办??
  6. 7.1 elementui的radio无法选中问题
  7. OpenShift 4 - 用CA证书或Token访问Internal Registry中的容器
  8. sublimeText3安装emmet(For Mac)
  9. 成都高新税务推出智能电话语音咨询服务 24小时在线答复
  10. MASM8.0 下载安装方法
  11. java 类方法中this_Java Eclipse 中 在类与方法调用中 (this)的用法
  12. 密码学的发展(第五篇:量子加密)
  13. centos7 vi保存退出_vi保存退出命令 - 卡饭网
  14. Excel 2016添加复选框
  15. Beta 反(tu)思(cao) 获小黄衫感言
  16. 6. Excel 图表制作
  17. CentOS系统FastDFS 配置和问题解决
  18. 如何评价一个开源项目(一)--活跃度
  19. 商务统计_4 用图表演示数据 - 频数分布
  20. 通信原理 数字基带信号之码间串扰

热门文章

  1. (转)Linux内核的Oops
  2. ajax:dataType
  3. Sql 语句:显示 Sql Server 中所有表中的信息
  4. [转载] 吴恩达机器学习逻辑回归练习题:逻辑回归及规则化(python实现)
  5. [转载] python int 幂函数_Python中对数和幂函数的不精确结果
  6. Linux学习第一篇之Linux系统安装——系统分区
  7. 11 旋转数组的最小数字
  8. Git正确的协作方式(很简单)
  9. Mac OS X Yosemite安装Hadoop 2.6记录
  10. 在XNA 3.0 项目添加声音——通过Xact播放简单的.wav文件