当前位置: 首页 > news >正文

网站开发 之cookie山西省建五公司官网

网站开发 之cookie,山西省建五公司官网,wordpress 登陆,专业网站定制团队一 使用线程池的好处 池化技术相比大家已经屡见不鲜了#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.w-s-a.com/news/9059/

相关文章:

  • 网站地图 seo中国建设招标网是私人网站吗
  • 高中作文网站全网营销有哪些平台
  • 网站构建建设制作平台上海搬家公司收费价目表
  • 成功案例展示网站做网站赚多少钱
  • 建设银行网站用什么字体网站建站后维护需要做哪些
  • 有哪些做平面设计好素材网站有哪些开网站建设
  • 国际交流网站平台有哪些筑建网
  • 网站程序是如何开发的江门市住房建设管理局网站
  • 网站建设一般需要几个步骤昵图网免费素材
  • 个人网站建设需求说明书微信域名防封在线生成
  • 专业网站建设的公司wordpress后台没有模板
  • 哈尔滨网站运营服务商制作外贸网站公司
  • 个人网站需要备案宁波网站推广工具
  • 苏州建设银行网站首页wordpress修改密码
  • 网站建设员工技能要求网站制作简单协议
  • 没有ipc备案的网站wordpress isux主题
  • 清远做网站电子商务网站建设需要的语言及特点6
  • 万州那家做网站c语言基础知识入门
  • 齐河网站建设公司价格网站建设包括什么
  • 论坛网站开发费用怎么把文件放到网站的根目录
  • 海南省零售户电商网站官渡区住房和城乡建设局网站
  • 怎么找淘宝客网站最新军事战况
  • 缺乏门户网站建设网页设计与制作项目教程第二版
  • 手机网站横竖屏一般做建设的是什么公司
  • 免费网站建设无广告网站开发 华景新城
  • 湖州网站制作报价西安网站开发有哪些公司
  • google 浏览器开源seo软件
  • 网站空间是什么意思自己怎样建设网站
  • 国外家装设计网站如何做软件开发
  • 凡科建站登录官网当当网网站建设策划书