线程池的简单介绍
来源:互联网 发布:哪个银行的淘宝卡最好 编辑:程序博客网 时间:2024/04/28 14:02
Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
(1) newCachedThreadPool
创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。示例代码如下:
- package test;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ThreadPoolExecutorTest {
- public static void main(String[] args) {
- ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
- for (int i = 0; i < 10; i++) {
- final int index = i;
- try {
- Thread.sleep(index * 1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- cachedThreadPool.execute(new Runnable() {
- public void run() {
- System.out.println(index);
- System.out.println("当前线程-->"+Thread.currentThread().getName());
- }
- });
- }
- }
- }
- /*结果:index==0
当前线程-->pool-1-thread-1 - index==1
当前线程-->pool-1-thread-1 - ...*/
- //都在线程1中执行
线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
(2) newFixedThreadPool
创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。示例代码如下:
- package test;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ThreadPoolExecutorTest {
- public static void main(String[] args) {
- ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
- for (int i = 0; i < 10; i++) {
- final int index = i;
- fixedThreadPool.execute(new Runnable() {
- public void run() {
- try {
- System.out.println(index);
- System.out.println("当前线程-->"+Thread.currentThread().getName());
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- }
- /**结果:
- index==0
当前线程-->pool-1-thread-1
index==1
当前线程-->pool-1-thread-2
index==2
当前线程-->pool-1-thread-3
index==3
当前线程-->pool-1-thread-4
index==4
当前线程-->pool-1-thread-1
index==5
当前线程-->pool-1-thread-2
index==6
当前线程-->pool-1-thread-3
index==7
当前线程-->pool-1-thread-4
index==8
当前线程-->pool-1-thread-1
index==9
当前线程-->pool-1-thread-2 - */
因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。
定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()
(3) newScheduledThreadPool
创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下:
- package test;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class ThreadPoolExecutorTest {
- public static void main(String[] args) {
- ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
- scheduledThreadPool.schedule(new Runnable() {
- public void run() {
- System.out.println("delay 3 seconds");
- }
- }, 3, TimeUnit.SECONDS);
- }
- }
表示延迟3秒执行。
定期执行示例代码如下:
- package test;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class ThreadPoolExecutorTest {
- public static void main(String[] args) {
- ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
- scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
- public void run() {
- System.out.println("hehe");
- System.out.println("当前线程-->"+Thread.currentThread().getName());
- }
- }, 1, 3, TimeUnit.SECONDS);
- }
- }
- /**结果:
- hehe
当前线程-->pool-1-thread-1
hehe
当前线程-->pool-1-thread-1
hehe
当前线程-->pool-1-thread-2
hehe
当前线程-->pool-1-thread-2
hehe
当前线程-->pool-1-thread-3
hehe
当前线程-->pool-1-thread-1
hehe
当前线程-->pool-1-thread-1 - */
- //运行的线程随机
表示延迟1秒后每3秒执行一次。
(4) newSingleThreadExecutor
创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。示例代码如下:
- package test;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ThreadPoolExecutorTest {
- public static void main(String[] args) {
- ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
- for (int i = 0; i < 10; i++) {
- final int index = i;
- singleThreadExecutor.execute(new Runnable() {
- public void run() {
- try {
- System.out.println(index);
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- }
- 线程池的简单介绍
- 线程的简单介绍
- 线程的简单介绍
- 线程的简单介绍
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- 线程池的介绍及简单实现
- zigbee智能家居系统转载文章
- 语文游戏项目涉及到的几个需求的解决方法
- tomcat都是按日保存日志文问分割
- iOS 懒加载模式
- Linux系统安装openssl扩展
- 线程池的简单介绍
- 双向认证第二步: p.12文件进行ssl双向认证
- 荒野之鹰
- eclipse生成的java项目中.settings的介绍
- MySQL 添加列,修改列,删除列
- 多通道图像的通道分享和合成函数-split、merge
- 16.7.11总结
- hdu 1257 最少拦截系统
- java接口实现汽车销售,显示销售车型和售价以及总销售金额