java 四种线程池newcachedthreadpool,newfixedthreadpool,newscheduledthreadpool,newsinglethreadexecutor
new thread的弊端
a. 每次new thread新建对象性能差。
b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom。
c. 缺乏更多功能,如定时执行、定期执行、线程中断。
相比new thread,java提供的四种线程池的好处在于:
a. 重用存在的线程,减少对象创建、消亡的开销,性能佳。
b. 可有效控制最大并发线程数,提高系统资源的使用率,同时避免过多资源竞争,避免堵塞。
c. 提供定时执行、定期执行、单线程、并发数控制等功能。
java通过executors提供四种线程池,分别为:
newcachedthreadpool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newfixedthreadpool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newscheduledthreadpool 创建一个定长线程池,支持定时及周期性任务执行。
newsinglethreadexecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(fifo, lifo, 优先级)执行。
(1). newcachedthreadpool
创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。示例代码如下:
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() { @overridepublic void run() {system.out.println(index);}});}
线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
(2). newfixedthreadpool
创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。示例代码如下:
executorservice fixedthreadpool = executors.newfixedthreadpool(3);for (int i = 0; i < 10; i++) {final int index = i;fixedthreadpool.execute(new runnable() { @overridepublic void run() {try {system.out.println(index);thread.sleep(2000);} catch (interruptedexception e) {// todo auto-generated catch blocke.printstacktrace();}}});}
因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。
定长线程池的大小最好根据系统资源进行设置。如runtime.getruntime().availableprocessors()。可参考preloaddatacache。
(3) newscheduledthreadpool
创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下:scheduledexecutorservice scheduledthreadpool =
executors.newscheduledthreadpool(5);scheduledthreadpool.schedule(new runnable() { @overridepublic void run() {system.out.println("delay 3 seconds");}}, 3, timeunit.seconds);
表示延迟3秒执行。
定期执行示例代码如下:
scheduledthreadpool.scheduleatfixedrate(new runnable() { @overridepublic void run() {system.out.println("delay 1 seconds, and excute every 3 seconds");}}, 1, 3, timeunit.seconds);
表示延迟1秒后每3秒执行一次。
scheduledexecutorservice比timer更安全,功能更强大,后面会有一篇单独进行对比。
(4)、newsinglethreadexecutor
创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(fifo, lifo, 优先级)执行。示例代码如下:
executorservice singlethreadexecutor = executors.newsinglethreadexecutor();for (int i = 0; i < 10; i++) {final int index = i;singlethreadexecutor.execute(new runnable() { @overridepublic void run() {try {system.out.println(index);thread.sleep(2000);} catch (interruptedexception e) {// todo auto-generated catch blocke.printstacktrace();}}});}
结果依次输出,相当于顺序执行各个任务。
现行大多数gui程序都是单线程的。android中单线程可用于数据库操作,文件操作,应用批量安装,应用批量删除等不适合并发但可能io阻塞性及影响ui线程响应的操作。
线程池的作用:
线程池作用就是限制系统中执行线程的数量。
根 据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排 队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池 中有等待的工作线程,就可以开始运行了;否则进入等待队列。
为什么要用线程池:
1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。
2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1mb内存,线程开的越多,消耗的内存也就越大,最后死机)。
java里面线程池的顶级接口是executor,但是严格意义上讲executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是executorservice。
相关文章:
java中线程池的图文代码详解
java并发编程之线程池的使用方法详解
相关视频:
java多线程与并发库高级应用视频教程
以上就是java并发包:java通过executors提供的四种线程池的详细内容。