在java中,线程池被用来管理线程的创建、维护和销毁等任务。线程池中包含了一组线程和一个任务队列,当有任务需要执行时,线程池中的线程会自动获取任务并执行,任务执行完毕后,线程也会被回收到线程池中重复利用。
Java中的线程池API提供了一个Executors类来帮助我们创建线程池,提供了四种线程池的实现方式:FixedThreadPool、CachedThreadPool、SingleThreadExecutor和ScheduledThreadPool。
FixedThreadPool
固定大小的线程池,只有在工作线程数没有达到线程池大小的时候才会新建线程来执行任务。线程池可以通过构造函数指定最大线程数,如果没有指定,则默认为Integer.MAX_VALUE。
示例代码:
立即学习“Java免费学习笔记(深入)”;
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
executorService.execute(()->{
System.out.println(Thread.currentThread().getName()+" is executing task ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}运行结果:
pool-1-thread-1 is executing task pool-1-thread-3 is executing task pool-1-thread-5 is executing task pool-1-thread-2 is executing task pool-1-thread-4 is executing task pool-1-thread-5 is executing task pool-1-thread-3 is executing task pool-1-thread-1 is executing task pool-1-thread-2 is executing task pool-1-thread-4 is executing task
CachedThreadPool
可缓存的线程池,当线程数超过了当前需要的数量时,会将多余的线程回收到线程池中,不需要的时候会自动销毁。如果线程池没有可用的线程,又有新的任务到来,线程池会创建一个新的线程来执行任务,直到线程池大小达到了Integer.MAX_VALUE的限制。
示例代码:
立即学习“Java免费学习笔记(深入)”;
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executorService.execute(()->{
System.out.println(Thread.currentThread().getName()+" is executing task ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}运行结果:
pool-1-thread-1 is executing task pool-1-thread-2 is executing task pool-1-thread-3 is executing task pool-1-thread-4 is executing task pool-1-thread-5 is executing task pool-1-thread-6 is executing task pool-1-thread-7 is executing task pool-1-thread-8 is executing task pool-1-thread-9 is executing task pool-1-thread-10 is executing task
SingleThreadExecutor
BJXShop网上购物系统是一个高效、稳定、安全的电子商店销售平台,经过近三年市场的考验,在中国网购系统中属领先水平;完善的订单管理、销售统计系统;网站模版可DIY、亦可导入导出;会员、商品种类和价格均实现无限等级;管理员权限可细分;整合了多种在线支付接口;强有力搜索引擎支持... 程序更新:此版本是伴江行官方商业版程序,已经终止销售,现于免费给大家使用。比其以前的免费版功能增加了:1,整合了论坛
单线程的线程池,只有一个工作线程,可以保证所有任务按照指定的顺序来执行(FIFO、LIFO、优先级等),相当于一个特殊的FixedThreadPool。
示例代码:
立即学习“Java免费学习笔记(深入)”;
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
executorService.execute(()->{
System.out.println(Thread.currentThread().getName()+" is executing task ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}运行结果:
pool-1-thread-1 is executing task pool-1-thread-1 is executing task pool-1-thread-1 is executing task ......
ScheduledThreadPool
定时调度的线程池,可以按照指定的延迟时间或周期性地来执行任务,可以实现定时任务或者周期性任务。线程池的大小可以指定,如果没有指定则默认为Integer.MAX_VALUE。
示例代码:
立即学习“Java免费学习笔记(深入)”;
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
ScheduledFuture> future = scheduledExecutorService.schedule(()->{
System.out.println(Thread.currentThread().getName()+" is executing delay task ");
}, 5, TimeUnit.SECONDS);
scheduledExecutorService.scheduleAtFixedRate(()->{
System.out.println(Thread.currentThread().getName()+" is executing periodic task ");
}, 2, 3, TimeUnit.SECONDS);运行结果:
pool-1-thread-1 is executing periodic task pool-1-thread-2 is executing periodic task pool-1-thread-3 is executing periodic task pool-1-thread-1 is executing periodic task pool-1-thread-3 is executing periodic task pool-1-thread-2 is executing periodic task pool-1-thread-3 is executing periodic task pool-1-thread-2 is executing periodic task ...... pool-1-thread-1 is executing delay task
总结
线程池在多线程开发中是一个极其重要的概念,可以有效地减少线程的创建、销毁和上下文切换等开销,提升系统性能和可维护性。Java中提供了Executors类来方便地创建线程池,并提供了不同的实现方式来应对不同的应用场景。开发人员在使用线程池的时候需要根据具体的需求和负载情况,选择合适的实现方式来达到最佳的性能和效果。










