- 浏览: 984349 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
线程池多余任务的拒绝执行策略有四中,分别是直接丢弃任务DiscardPolicy,
丢弃old线程任务DiscardOldestPolicy,抛出异常AbortPolicy和调用线程执行多余任务CallerRunsPolicy,
下面我们用一个实例来测试一下这几种策略:
测试实例:
分别使用四中多余任务执行策略,
直接丢弃任务DiscardPolicy:
控制台输出:多余任务直接丢弃
time:1490366101248 -> ==========before sleep
time:1490366101248 -> run task:0 -> pool-1-thread-1
time:1490366102250 -> run over:0 -> pool-1-thread-1
time:1490366102250 -> run task:1 -> pool-1-thread-1
time:1490366103251 -> run over:1 -> pool-1-thread-1
time:1490366105250 -> ==========before shutdown()
time:1490366105251 -> ==========after shutdown(),pool.isTerminated=false
time:1490366105253 -> ==========now,pool.isTerminated=true
丢弃old线程任务DiscardOldestPolicy:
控制台输出:直接丢弃旧的任务
time:1490366209872 -> run task:0 -> pool-1-thread-1
time:1490366209872 -> ==========before sleep
time:1490366210872 -> run over:0 -> pool-1-thread-1
time:1490366210872 -> run task:9 -> pool-1-thread-1
time:1490366211875 -> run over:9 -> pool-1-thread-1
time:1490366213872 -> ==========before shutdown()
time:1490366213873 -> ==========after shutdown(),pool.isTerminated=false
time:1490366213874 -> ==========now,pool.isTerminated=true
抛出异常:AbortPolicy
控制台输出:抛出异常
time:1490366258974 -> run task:0 -> pool-1-thread-1Exception in thread "main"
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@3639b3a2 rejected from java.util.concurrent.ThreadPoolExecutor@684be8b8[Running, pool size = 1, active threads = 1, queued tasks = 1, 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 juc.executor.TestRejectedExecutionHandler.main(TestRejectedExecutionHandler.java:36)
time:1490366259974 -> run over:0 -> pool-1-thread-1
time:1490366259974 -> run task:1 -> pool-1-thread-1
time:1490366260976 -> run over:1 -> pool-1-thread-1
调用线程执行多余任务:CallerRunsPolicy
控制台输出:调用线程执行多余任务
time:1490366322359 -> run task:0 -> pool-1-thread-1
time:1490366322359 -> run task:2 -> main
time:1490366323360 -> run over:0 -> pool-1-thread-1
time:1490366323360 -> run over:2 -> main
time:1490366323360 -> run task:1 -> pool-1-thread-1
time:1490366323360 -> run task:4 -> main
time:1490366324361 -> run over:4 -> main
time:1490366324361 -> run over:1 -> pool-1-thread-1
time:1490366324361 -> run task:5 -> main
time:1490366324361 -> run task:3 -> pool-1-thread-1
time:1490366325362 -> run over:5 -> main
time:1490366325362 -> run task:7 -> main
time:1490366325363 -> run over:3 -> pool-1-thread-1
time:1490366325363 -> run task:6 -> pool-1-thread-1
time:1490366326363 -> run over:7 -> main
time:1490366326363 -> run over:6 -> pool-1-thread-1
time:1490366326363 -> run task:8 -> pool-1-thread-1
time:1490366326363 -> run task:9 -> main
time:1490366327365 -> run over:9 -> main
time:1490366327365 -> run over:8 -> pool-1-thread-1
time:1490366327365 -> ==========before sleep
time:1490366331366 -> ==========before shutdown()
time:1490366331367 -> ==========after shutdown(),pool.isTerminated=false
time:1490366331367 -> ==========now,pool.isTerminated=true
丢弃old线程任务DiscardOldestPolicy,抛出异常AbortPolicy和调用线程执行多余任务CallerRunsPolicy,
下面我们用一个实例来测试一下这几种策略:
测试实例:
package juc.executor; import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; import java.util.concurrent.TimeUnit; /** * 测试线程池拒绝执行任务策略 * * @author donald 2017年3月24日 上午8:56:07 */ public class TestRejectedExecutionHandler { /** * * @param msg */ static void log(String msg) { System.out.println("time:"+System.currentTimeMillis() + " -> " + msg); } public static void main(String[] args) throws Exception { ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); //直接丢弃任务 pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); //丢弃old线程任务 // pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); //抛出异常 // pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); //调用线程执行多余任务 // pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); for (int i = 0; i < 10; i++) { final int index = i; pool.submit(new Runnable() { public void run() { log("run task:" + index + " -> " + Thread.currentThread().getName()); try { Thread.sleep(1000L); } catch (Exception e) { e.printStackTrace(); } log("run over:" + index + " -> " + Thread.currentThread().getName()); } }); } log("==========before sleep"); Thread.sleep(4000L); log("==========before shutdown()"); pool.shutdown(); log("==========after shutdown(),pool.isTerminated=" + pool.isTerminated()); pool.awaitTermination(1000L, TimeUnit.SECONDS); log("==========now,pool.isTerminated=" + pool.isTerminated()); } }
分别使用四中多余任务执行策略,
直接丢弃任务DiscardPolicy:
控制台输出:多余任务直接丢弃
time:1490366101248 -> ==========before sleep
time:1490366101248 -> run task:0 -> pool-1-thread-1
time:1490366102250 -> run over:0 -> pool-1-thread-1
time:1490366102250 -> run task:1 -> pool-1-thread-1
time:1490366103251 -> run over:1 -> pool-1-thread-1
time:1490366105250 -> ==========before shutdown()
time:1490366105251 -> ==========after shutdown(),pool.isTerminated=false
time:1490366105253 -> ==========now,pool.isTerminated=true
丢弃old线程任务DiscardOldestPolicy:
控制台输出:直接丢弃旧的任务
time:1490366209872 -> run task:0 -> pool-1-thread-1
time:1490366209872 -> ==========before sleep
time:1490366210872 -> run over:0 -> pool-1-thread-1
time:1490366210872 -> run task:9 -> pool-1-thread-1
time:1490366211875 -> run over:9 -> pool-1-thread-1
time:1490366213872 -> ==========before shutdown()
time:1490366213873 -> ==========after shutdown(),pool.isTerminated=false
time:1490366213874 -> ==========now,pool.isTerminated=true
抛出异常:AbortPolicy
控制台输出:抛出异常
time:1490366258974 -> run task:0 -> pool-1-thread-1Exception in thread "main"
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@3639b3a2 rejected from java.util.concurrent.ThreadPoolExecutor@684be8b8[Running, pool size = 1, active threads = 1, queued tasks = 1, 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 juc.executor.TestRejectedExecutionHandler.main(TestRejectedExecutionHandler.java:36)
time:1490366259974 -> run over:0 -> pool-1-thread-1
time:1490366259974 -> run task:1 -> pool-1-thread-1
time:1490366260976 -> run over:1 -> pool-1-thread-1
调用线程执行多余任务:CallerRunsPolicy
控制台输出:调用线程执行多余任务
time:1490366322359 -> run task:0 -> pool-1-thread-1
time:1490366322359 -> run task:2 -> main
time:1490366323360 -> run over:0 -> pool-1-thread-1
time:1490366323360 -> run over:2 -> main
time:1490366323360 -> run task:1 -> pool-1-thread-1
time:1490366323360 -> run task:4 -> main
time:1490366324361 -> run over:4 -> main
time:1490366324361 -> run over:1 -> pool-1-thread-1
time:1490366324361 -> run task:5 -> main
time:1490366324361 -> run task:3 -> pool-1-thread-1
time:1490366325362 -> run over:5 -> main
time:1490366325362 -> run task:7 -> main
time:1490366325363 -> run over:3 -> pool-1-thread-1
time:1490366325363 -> run task:6 -> pool-1-thread-1
time:1490366326363 -> run over:7 -> main
time:1490366326363 -> run over:6 -> pool-1-thread-1
time:1490366326363 -> run task:8 -> pool-1-thread-1
time:1490366326363 -> run task:9 -> main
time:1490366327365 -> run over:9 -> main
time:1490366327365 -> run over:8 -> pool-1-thread-1
time:1490366327365 -> ==========before sleep
time:1490366331366 -> ==========before shutdown()
time:1490366331367 -> ==========after shutdown(),pool.isTerminated=false
time:1490366331367 -> ==========now,pool.isTerminated=true
发表评论
-
Executors解析
2017-04-07 14:38 1255ThreadPoolExecutor解析一(核心线程池数量、线 ... -
ScheduledThreadPoolExecutor解析三(关闭线程池)
2017-04-06 20:52 4455ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析二(任务调度)
2017-04-06 12:56 2127ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析一(调度任务,任务队列)
2017-04-04 22:59 4992Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析四(线程池关闭)
2017-04-03 23:02 9111Executor接口的定义:http: ... -
ThreadPoolExecutor解析三(线程池执行提交任务)
2017-04-03 12:06 6087Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析二(线程工厂、工作线程,拒绝策略等)
2017-04-01 17:12 3041Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析一(核心线程池数量、线程池状态等)
2017-03-31 22:01 20520Executor接口的定义:http://donald-dra ... -
ScheduledExecutorService接口定义
2017-03-29 12:53 1510Executor接口的定义:http://donald-dra ... -
AbstractExecutorService解析
2017-03-29 08:27 1080Executor接口的定义:http: ... -
ExecutorCompletionService解析
2017-03-28 14:27 1593Executor接口的定义:http://donald-dra ... -
CompletionService接口定义
2017-03-28 12:39 1068Executor接口的定义:http://donald-dra ... -
FutureTask解析
2017-03-27 12:59 1329package java.util.concurrent; ... -
Future接口定义
2017-03-26 09:40 1198/* * Written by Doug Lea with ... -
ExecutorService接口定义
2017-03-25 22:14 1162Executor接口的定义:http://donald-dra ... -
Executor接口的定义
2017-03-24 23:24 1676package java.util.concurrent; ... -
JAVA集合类简单综述
2017-03-23 22:51 925Queue接口定义:http://donald-draper. ... -
DelayQueue解析
2017-03-23 11:00 1737Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析下-TransferQueue
2017-03-22 22:20 2140Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析上-TransferStack
2017-03-21 22:08 3059Queue接口定义:http://donald-draper. ...
相关推荐
6. 如果线程池中所有线程都在执行任务,新的Runnable任务将被拒绝 Java线程池的特点: * 可以重用线程,减少创建和销毁线程的开销 * 可以控制线程池的大小,避免创建太多的线程 * 可以设置线程池的等待队列,避免...
在`TheadPoolTest1`这个测试文件中,可能包含了对线程池的初始化、任务提交、执行流程跟踪以及各种场景下的测试,比如不同负载情况下的性能比较、拒绝策略的验证等。通过分析这个源码,我们可以深入理解线程池的运作...
1. **ExecutorService**: 这是线程池的核心接口,定义了线程池的主要操作,如执行任务、关闭线程池等。通过`execute()` 方法,我们可以提交一个Runnable任务到线程池。 2. **ThreadPoolExecutor**: 这是...
达到最大线程数后,线程池会拒绝新提交的任务,具体策略可自定义(如抛出异常、丢弃最旧任务或丢弃新任务)。 4. **线程回收**:当线程空闲时间超过设定值,线程池会销毁这些线程,以减少资源占用。核心线程不会被...
为了避免这种情况,可以在创建线程池时指定一个合适的拒绝策略,比如使用`AbortPolicy`来终止程序,或者使用`CallerRunsPolicy`让调用者直接执行任务。 另一种线程泄漏的情形发生在工作线程执行长时间阻塞的任务时...
二、线程池拒绝策略 在 SpringBoot 应用程序中,线程池的拒绝策略是非常重要的。拒绝策略决定了当线程池达到最大线程数时,如何处理新的任务请求。 在本文的案例中,我们使用了 `AbortPolicy` 作为拒绝策略,该...
4. 配置拒绝策略:当工作队列满且无法接受新任务时,线程池会根据预设的拒绝策略处理新任务。 在"处理器threadPool deleteR插件"的描述中,可能指的是一个用于删除操作的线程池插件。这样的插件可能用于高效并行地...
线程池在IT行业中是一种非常重要的并发处理机制,它能够高效地管理和调度多个并发执行的任务。在本项目“线程池画风筝动态效果”中,我们利用线程池技术来实现风筝在屏幕上转动的动画效果,以此来直观地展示线程池...
线程池的拒绝策略也是需要关注的,例如AbortPolicy会抛出异常,DiscardPolicy会丢弃任务,DiscardOldestPolicy会丢弃最旧的任务,而CallerRunsPolicy会让调用者线程执行任务。 除了理论知识,通过编写实际的多线程...
如果队列也满了,那么`ThreadPoolExecutor`会拒绝执行任务,可以通过异常处理策略来处理这种情况。 线程池的管理通常包括以下几个方面: 1. 初始化线程池,设置合适的参数。 2. 提交任务到线程池,`executor....
- 服务类:包含了使用线程池执行任务的代码。 - 测试类:用于验证线程池的正确性和性能。 通过这个示例,开发者可以学习到如何在实际项目中合理地利用线程池,优化并发性能,同时避免资源浪费。理解并掌握Spring...
5. **线程池扩展**:如果线程池中的线程都忙于执行任务,而新任务还在不断提交,线程池可能根据预设的策略动态增加线程数量。 6. **线程池收缩**:当任务量减少时,线程池可能会减少线程数量,避免资源浪费。 描述...
线程池维护着一组工作线程,当有新的任务提交时,线程池会根据当前的工作负载情况决定是否创建新线程,或者复用已有线程来执行任务。这种机制可以控制并发程度,防止过多线程导致系统资源过度消耗,提高系统性能和...
在Java并发库中,`ScheduledExecutorService`接口提供了一种定时执行任务的能力,这对于实现定时任务非常有用。例如,可以设定每隔一段时间执行一次任务,或者在特定时间点执行。在`TestScheduledThread`示例中,...
4. **超时策略**:当线程池和队列都满时,如何处理新任务,比如拒绝服务、丢弃最旧的任务或创建新的线程。 在Linux下,我们可以使用Java的`java.util.concurrent.ThreadPoolExecutor`类来创建线程池,也可以使用...
4. **ScheduledThreadPool**:定时及周期性执行任务的线程池,支持延迟和定时执行。 理解并熟练掌握这些知识对于编写高效、稳定的多线程Java应用至关重要。通过阅读附带的源码和中文教程,你可以更深入地学习如何在...
在什么(What)线程中执行任务? 任务按照什么(What)顺序执行(FIFO、LIFO、优先级)? 有多少个(How Many)任务能并发执行? 在队列中有多少个(How Many)任务在等待执行? 如果系统...
7. **并发工具类**:如`CyclicBarrier`和`Phaser`用于多线程间的同步,`ThreadPoolExecutor`的`RejectedExecutionHandler`处理拒绝策略,`ScheduledExecutorService`用于定时任务等。 8. **并发模式**:测试可能...
当请求超出线程池和队列的承载能力时,会按照`CallerRunsPolicy`策略处理,即由提交请求的线程来执行任务,防止任务丢失。 结合SpringMVC的RESTful服务和线程池,开发者可以构建出高效、可扩展的Web服务,能够处理...
常见的ExecutorService实现有ThreadPoolExecutor,它允许自定义线程池的大小、工作队列、拒绝策略等。ThreadPoolExecutor可以配合BlockingQueue使用,例如,将待执行的任务放入BlockingQueue,由线程池中的工作线程...