Java中线程池的使用
2021/8/30 22:06:08
本文主要是介绍Java中线程池的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.缓存线程池(长度无限制)
执行流程:判断线程池是否存在空闲线程
存在则使用
不存在,则创建线程,并放入线程池
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPool { //缓存线程池 public static void main(String[] args) { ExecutorService ex = Executors.newCachedThreadPool(); ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"锄禾日当日"); } }); ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"锄禾日当日"); } }); ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"锄禾日当日"); } }); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"锄禾日当日"); } }); } }
执行结果:
2.定长线程池(长度是指定的数值)
执行流程:1.判断线程池是否存在空闲线程
2.存在则使用
3.不存在空闲线程,且线程池未满的,则创建线程并放入线程池,然后使用
4.不存在空闲线程,且线程池已满的情况下,则等待线程池存在空闲线程
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolFix { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"窗前民航业"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }); executorService.execute(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"窗前民航业"); } }); executorService.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"hehheheh"); } }); } }
执行结果
3.单线程池
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SingleThreadPool { public static void main(String[] args) { ExecutorService ex = Executors.newSingleThreadExecutor(); ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"hehehehe"); } }); ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"wwwwww"); } }); ex.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"hhhhhh"); } }); } }
执行结果
4.周期性定长线程池
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ThreadPoolSecd { public static void main(String[] args) { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2); // scheduledExecutorService.schedule(new Runnable() { // @Override // public void run() { // System.out.println(Thread.currentThread().getName()+"原来如此"); // } // },5, TimeUnit.SECONDS); scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"旱地和西游"); } },5,2,TimeUnit.SECONDS ); scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"旱地和红楼"); } },5,2,TimeUnit.SECONDS ); } }
这篇关于Java中线程池的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-12百万架构师第十五课:源码分析:Spring 源码分析:SpringMVC核心原理及源码分析|JavaGuide
- 2025-01-11有哪些好用的家政团队管理工具?
- 2025-01-11营销人必看的GTM五个指标
- 2025-01-11办公软件在直播电商前期筹划中的应用与推荐
- 2025-01-11提升组织效率:上级管理者如何优化跨部门任务分配
- 2025-01-11酒店精细化运营背后的协同工具支持
- 2025-01-11跨境电商选品全攻略:工具使用、市场数据与选品策略
- 2025-01-11数据驱动酒店管理:在线工具的核心价值解析
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide