设计师常用的图库网站,亚马逊网站首页,竞价系统,做网站如何下载别人网站图片一 使用线程池的好处 池化技术相比大家已经屡见不鲜了#xff0c;线程池、数据库连接池、Http 连接池等等都是对这个思想的应用。池化技术的思想主要是为了减少每次获取资源的消耗#xff0c;提高对资源的利用率。 线程池提供了一种限制和管理资源#xff08;包括执行一个任…一 使用线程池的好处 池化技术相比大家已经屡见不鲜了线程池、数据库连接池、Http 连接池等等都是对这个思想的应用。池化技术的思想主要是为了减少每次获取资源的消耗提高对资源的利用率。 线程池提供了一种限制和管理资源包括执行一个任务。 每个线程池还维护一些基本统计信息例如已完成任务的数量。
这里借用《Java 并发编程的艺术》提到的来说一下使用线程池的好处
降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的消耗。提高响应速度。当任务到达时任务可以不需要的等到线程创建就能立即执行。提高线程的可管理性。线程是稀缺资源如果无限制的创建不仅会消耗系统资源还会降低系统的稳定性使用线程池可以进行统一的分配调优和监控。
二、线程池的核心参数 corePoolSize 线程池核心线程大小 maximumPoolSize 线程池最大线程数量 keepAliveTime 空闲线程存活时间 unit 空闲线程存活时间单位 workQueue 工作队列 threadFactory 线程工厂 handler 拒绝策略 三、Runnable和ThreadPoolExecutor的使用
1.首先创建一个 Runnable 接口的实现类当然也可以是 Callable 接口我们上面也说了两者的区别。MyRunnable.java
package com.newstart.controller;import java.util.Date;public class MyRunnable implements Runnable{private String command;public MyRunnable(String s) {this.command s;}Overridepublic void run() {System.out.println(Thread.currentThread().getName() Start. Time new Date());processCommand();System.out.println(Thread.currentThread().getName() End. Time new Date());}private void processCommand() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}Overridepublic String toString() {return this.command;}
}2.编写测试程序我们这里以阿里巴巴推荐的使用 ThreadPoolExecutor 构造函数自定义参数的方式来创建线程池。ThreadPoolExecutorDemo.java
package com.newstart.controller;import java.util.concurrent.*;public class ThreadPoolExecutorDemo {private static final int CORE_POOL_SIZE 5;private static final int MAX_POOL_SIZE 10;private static final int QUEUE_CAPACITY 100;private static final Long KEEP_ALIVE_TIME 1L;public static void main(String[] args) throws InterruptedException {//使用阿里巴巴推荐的创建线程池的方式//通过ThreadPoolExecutor构造函数自定义参数创建ThreadPoolExecutor executor new ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE_TIME,TimeUnit.SECONDS,new ArrayBlockingQueue(QUEUE_CAPACITY),new ThreadPoolExecutor.CallerRunsPolicy());for (int i0;i10;i){//创建WorkerThread对象WorkerThread类实现了Runnable 接口Runnable worker new MyRunnable( i);//执行Runnableexecutor.execute(worker);}//终止线程池executor.shutdown();while (!executor.isTerminated()) {}System.out.println(Finished all threads);}
}四、使用Callable和和ThreadPoolExecutor的使用
1.首先创建一个 Callable 接口的实现类MyCallable.java
package com.newstart.controller;import java.util.Date;
import java.util.concurrent.Callable;public class MyCallble implements Callable {private String command;public MyCallble(String s) {this.command s;}private void processCommand() {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}Overridepublic String toString() {return this.command;}Overridepublic Object call() throws Exception {System.out.println(Thread.currentThread().getName() Start. Time new Date());processCommand();System.out.println(Thread.currentThread().getName() End. Time new Date());return Thread.currentThread().getName();}
}2.编写测试程序我们这里以阿里巴巴推荐的使用 ThreadPoolExecutor 构造函数自定义参数的方式来创建线程池。ThreadPoolExecutorDemo.java
package com.newstart.controller;import java.util.concurrent.*;public class ThreadPoolExecutorDemo {private static final int CORE_POOL_SIZE 5;private static final int MAX_POOL_SIZE 10;private static final int QUEUE_CAPACITY 100;private static final Long KEEP_ALIVE_TIME 1L;public static void main(String[] args) throws InterruptedException {//使用阿里巴巴推荐的创建线程池的方式//通过ThreadPoolExecutor构造函数自定义参数创建ThreadPoolExecutor executor new ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE_TIME,TimeUnit.SECONDS,new ArrayBlockingQueue(QUEUE_CAPACITY),new ThreadPoolExecutor.CallerRunsPolicy());for (int i0;i10;i){//创建WorkerThread对象WorkerThread类实现了Runnable 接口Callable worker new MyCallble( i);//执行RunnableFuture future executor.submit(worker);Thread.sleep(1000);System.out.println(future.isDone());}//终止线程池executor.shutdown();while (!executor.isTerminated()) {}System.out.println(Finished all threads);}
}Callable和Runnable的区别 Runnable无返回值 Callable有返回值并且可以抛出异常 在线程池中 对于Callable接口需要使用submit执行并且返回值为future通过future的isdone方法可以判断线程是否执行完毕 对于Runnable接口需要使用execute执行 shutdown()和 shutdownNow()的区别 shutdown :关闭线程池线程池的状态变为 SHUTDOWN。线程池不再接受新任务了但是队列里的任务得执行完毕。shutdownNow :关闭线程池线程的状态变为 STOP。线程池会终止当前正在运行的任务并停止处理排队的任务并返回正在等待执行的 List。 文章转载自: http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.bwygy.cn.gov.cn.bwygy.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.hhpbj.cn.gov.cn.hhpbj.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.mnlk.cn.gov.cn.mnlk.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.snbq.cn.gov.cn.snbq.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com