java ThreadPoolExecutor 自定义线程池demo
来源:互联网 发布:电影知无涯者拉马努金 编辑:程序博客网 时间:2024/05/01 01:19
package com.lyq.jsoup.concurrent.threadpool;import java.util.concurrent.TimeUnit;/** * Created by mike on 2016/12/28. */public class TestThreadPoolTask implements Runnable { private int id ; private String name; public TestThreadPoolTask(int id, String name) { this.id = id; this.name = name; } @Override public void run() { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ",id:" +this.id); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
package com.lyq.jsoup.concurrent.threadpool;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.LinkedBlockingDeque;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;/** * Created by mike on 2016/12/28. */public class TestThreadPool { public static void main(String[] args){ ArrayBlockingQueue<Runnable> arrayWorkQueue = new ArrayBlockingQueue(10); LinkedBlockingDeque<Runnable> linkedWorkQueue = new LinkedBlockingDeque(); int count = 20; ExecutorService threadPool = new ThreadPoolExecutor(5, //corePoolSize线程池中核心线程数 10, //maximumPoolSize 线程池中最大线程数 60, //线程池中线程的最大空闲时间,超过这个时间空闲线程将被回收 TimeUnit.SECONDS,//时间单位 //下面是采用有界队列和无界队列的区别 arrayWorkQueue, //linkedWorkQueue, //下面是jdk的四种执行策略 //new ThreadPoolExecutor.AbortPolicy() 这种策略直接抛出异常,丢弃任务。 //new ThreadPoolExecutor.DiscardPolicy() 这种策略和AbortPolicy几乎一样,也是丢弃任务,只不过他不抛出异常。 //new ThreadPoolExecutor.CallerRunsPolicy() //线程调用运行该任务的 execute 本身。此策略提供简单的反馈控制机制,能够减缓新任务的提交速度。没看明白,当时是我的main线程执行的task5 new ThreadPoolExecutor.DiscardOldestPolicy()//如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程) ); for (int i = 1; i <= count ;i++){ TestThreadPoolTask task = new TestThreadPoolTask(i,"name"+i); threadPool.execute(task); } threadPool.shutdown(); }}
通过上面的小demo可以总结规律
1:如果自定义队列是有界队列:
当线程池中的任务数 > maximumPoolSize时,线程池会自动创建线程,直到 线程数 = maximumPoolSize;
如果这个时候线程池任务还 > maximumPoolSize, 线程池就会把任务放到任务队列中;
如果队列满了采用配置的拒绝策略
2:如果自定义队列是无解队列:
maximumPoolSize 参数无效,线程池中就只有corePoolSize个线程执行任务。知道系统崩溃(内存溢出,由于是无解队列)
3:拒绝策略建议采用自定义拒绝策略
0 0
- java ThreadPoolExecutor 自定义线程池demo
- 自定义Java线程池 ThreadPoolExecutor
- 自定义Java线程池 ThreadPoolExecutor
- Java自定义线程池-ThreadPoolExecutor
- ThreadPoolExecutor 线程池Demo
- java ThreadPoolExecutor 自定义线程池优势
- 自定义线程池ThreadPoolExecutor
- 自定义线程池---ThreadPoolExecutor
- Java线程池ThreadPoolExecutor及自定义线程池
- Java 线程池ThreadPoolExecutor
- Java 线程池ThreadPoolExecutor
- Java 线程池ThreadPoolExecutor
- JAVA线程池:ThreadPoolExecutor
- JAVA线程池ThreadPoolExecutor
- JAVA线程池ThreadPoolExecutor
- ThreadPoolExecutor java 线程池
- Java 线程池ThreadPoolExecutor
- java 线程池ThreadPoolExecutor
- typeof 的返回值有那些?
- Jfinal源码详解
- usb工作过程
- java常用设计模式应用案例
- windows下adb shell的使用,adb shell的概念
- java ThreadPoolExecutor 自定义线程池demo
- 指针
- Only the original thread that created a view hierarchy can touch its views:
- linux下redis安装、启动与停止,redis做成服务
- 使用spark ml pipeline进行机器学习
- HDFS中的TransactionsSinceLastCheckpoint
- 关于 Installation failed with message INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION.
- sql优化的一些总结
- 分享一下简单的android SharedPreferences本地存储数据工具类