ThreadPoolExecutor介绍
ThreadPoolExecutor的说明
ThreadPoolExecutor常见的操作主要有以下几个方法:
getPoolSize()
:返回线程池实际的线程数。getActiveCount()
:返回在执行者中正在执行任务的线程数。getCompletedTaskCount()
:返回执行者完成的任务数。submit()
: 提交一个线程给线程执行者,如果执行者有空余线程,则直接执行;否则等待直到有空闲线程。这里调用sumbit后,并不会阻塞调用线程。调用者所在的线程和执行的线程并发运行。
下面要着重介绍两个方法:
shutdown()
: 调用这个方法后,线程执行者在完成当前已经提交的所有任务后,结束运行。
a. 在主线程中如果调用线程执行者的这个方法,并不会使线程执行者中已经submit的任务中断(无论是待执行、执行中)
b. 调用shutdown会通知执行者,后面提交的任务“不允许接受”,在shutdown后提交任务,会抛出RejectedExecutionException
的异常信息。shutdownNow()
: 调用这个方法后,立即停止执行者的运行。返回待执行的Task。
a. 中断所有正在执行的线程: 正在执行的线程会收到中断信息。抛出InterruptedException
b. 后面提交的任务“不允许接受”,在shutdownNow后再调用submit提交任务,会抛出RejectedExecutionException
的异常信息。
code1:
public class ThreadPoolTest {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println("sub thread classs");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
executor.shutdown();
System.out.println("Main classs");
}
}
结果:
Main classs
sub thread classs
sub thread classs
sub thread classs
说明:
调用executor.submit后,并不会阻塞主线程。主线程和提交到执行者中的线程并发执行。
code2:
public class ThreadPoolTest {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println("sub thread classs");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
executor.shutdown();
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println("sub thread classs");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println("Main classs");
}
}
结果:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task
java.util.concurrent.FutureTask@1c910477 rejected from java.util.concurrent.
ThreadPoolExecutor@74bc2f47[Shutting down, pool size = 3, active threads = 3, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:110)
at ThreadPoolTest.ThreadPoolTest.main(ThreadPoolTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
sub thread classs
sub thread classs
sub thread classs
说明:
调用executor.shutdown()后,如果再调用submit()方法,调用线程会抛出rejectedExecution,如果没有try,cacth直接中断
后续的操作。
code3:
public class ThreadPoolTest {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println("sub thread classs");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
List<Runnable> list=executor.shutdownNow();
System.out.println("list size:"+list.size());
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println("sub thread classs");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println("Main classs");
}
}
结果:
list size:0
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at ThreadPoolTest.ThreadPoolTest$1.run(ThreadPoolTest.java:16)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at ThreadPoolTest.ThreadPoolTest$1.run(ThreadPoolTest.java:16)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at ThreadPoolTest.ThreadPoolTest$1.run(ThreadPoolTest.java:16)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task
java.util.concurrent.FutureTask@3a9d04dc rejected from java.util.concurrent.ThreadPoolExecutor@1aaa2594
[Shutting down, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 1]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:110)
at ThreadPoolTest.ThreadPoolTest.main(ThreadPoolTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
说明:
调用shutDownNow()后,直接中断所有的线程。并且后面submit()task会抛出rejectedExecution。
ThreadPoolExecutor介绍的更多相关文章
- (转)java中Executor、ExecutorService、ThreadPoolExecutor介绍
转自: http://blog.csdn.net/linghu_java/article/details/17123057 ScheduledThreadPoolExecutor介绍: http:// ...
- java线程池技术(二): 核心ThreadPoolExecutor介绍
版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程池技术属于比较"古老"而又比较基础的技术了,本篇博客主要作用是个人技术梳理,没什么新玩意. 一.Java线程池技术的 ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)
1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes th ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes the given c ...
- ThreadPoolExecutor解析
前言:在最新的阿里规范中强制使用ThreadPoolExecutor方式创建线程池,不允许使用Executors,因此有必要对ThreadPoolExecutor进行进一步了解. 1.ThreadPo ...
- Java并发编程总结5——ThreadPoolExecutor
一.ThreadPoolExecutor介绍 在jdk1.8中,构造函数有4个.以 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, ...
- ThreadPoolExecutor线程池解析与BlockingQueue的三种实现
目的 主要介绍ThreadPoolExecutor的用法,和较浅显的认识,场景的使用方案等等,比较忙碌,如果有错误还请大家指出 ThreadPoolExecutor介绍 ThreadPoolExecu ...
- 多线程编程学习十一(ThreadPoolExecutor 详解).
一.ThreadPoolExecutor 参数说明 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keep ...
- Android的线程和线程池
---恢复内容开始--- 一.Android线程的形态 (一)AsyncTask解析 AysncTask简介:①.实现上封装了Thread和Handler ②.不适合进行特别耗时的后台任务 Ays ...
随机推荐
- 仿QQ聊天软件2.0版
地址:http://blog.csdn.net/u012027907/article/details/36952893 Oracle java
- Android(java)学习笔记177:BroadcastReceiver之 应用程序安装和卸载 的广播接收者
国内的主流网络公司(比如网易.腾讯.百度等等),他们往往采用数据挖掘技术获取用户使用信息,从而采用靶向营销.比如电脑上,我们浏览网页的时候,往往会发现网页上会出现我们之前经常浏览内容的商 ...
- compareTo()
从字面意思可知这个方法就是比较的意思. 所以该方法有如下两种情况: 1.比较前后的两个字符不相同: (1) String str = "Hello World"; Stri ...
- 模版引擎(NVelocity)开发
在net中用模版开发,在handler中用到了大量的html代码.为解决这个问题,我可以采用模版引擎(NVelocity)进行开发.1.首先需要将NVelocity.dll文件放入项目,其次引用.2. ...
- 日志记录组件[Log4net]详细介绍
转载:http://www.cnblogs.com/liwei6797/archive/2007/04/27/729679.html 因为工作中有要用到Log记录,找到一篇不错的文章,就转了过来. 一 ...
- centos 彻底卸载mysql
yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...
- 第一章JSP基础语法
jsp页面元素构成 jsp页面组成部分有:指令,注释,静态内容,表达式,小脚本,声明. jsp指令 page指令:通常位于jsp页面的顶端,同一个页面可以有多个page指令 include指令:将一个 ...
- 读懂IL代码(四)
这一篇是IL系列的最后一篇的,主要是要说一下IL中的流程控制.我相信,经过前面三篇的介绍,看IL代码应该不是什么大问题了吧.好吧,闲话不多说了,就来简单的说一下吧. 还是跟前几篇一样,以例子来解释说明 ...
- 中文翻译:pjsip教程(三)之ICE stream transport的使用
1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...
- Windows Phone 之下拉菜单ListPicker
默认情况下,Visual Studio的ToolBox里没有任何下拉菜单的控件可供使用,虽然可以手工输入代码使用隐藏的ComboBox来实现下拉菜单,但是显示出来的菜单与Metro UI主题不匹配.S ...