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

青浦建设网站公司做商城网站的公司推荐

青浦建设网站公司,做商城网站的公司推荐,看案例网站,无锡专业网站推广一、使用Executors创建线程池之前创建线程的时候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()这三个方法。当然Executors也是用不同的参数去new ThreadPoolExecutor1. newFixedThreadPool()创建线程数固定大小的线程池。 由于使用…一、使用Executors创建线程池之前创建线程的时候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()这三个方法。当然Executors也是用不同的参数去new ThreadPoolExecutor1. newFixedThreadPool()创建线程数固定大小的线程池。 由于使用了LinkedBlockingQueue所以maximumPoolSize 没用当corePoolSize满了之后就加入到LinkedBlockingQueue队列中。每当某个线程执行完成之后就从LinkedBlockingQueue队列中取一个。所以这个是创建固定大小的线程池。public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue());}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), defaultHandler);}2.newSingleThreadPool()创建线程数为1的线程池由于使用了LinkedBlockingQueue所以maximumPoolSize 没用corePoolSize为1表示线程数大小为1,满了就放入队列中执行完了就从队列取一个。public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue()));}3.newCachedThreadPool()创建可缓冲的线程池。没有大小限制。由于corePoolSize为0所以任务会放入SynchronousQueue队列中SynchronousQueue只能存放大小为1所以会立刻新起线程由于maxumumPoolSize为Integer.MAX_VALUE所以可以认为大小为2147483647。受内存大小限制。public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue());}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), defaultHandler);}二、使用ThreadPoolExecutor创建线程池ThreadPoolExecutor的构造函数public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {if (corePoolSize maximumPoolSize  0 ||maximumPoolSize keepAliveTime throw new IllegalArgumentException();if (workQueue  null || threadFactory  null || handler  null)throw new NullPointerException();this.corePoolSize  corePoolSize;this.maximumPoolSize  maximumPoolSize;this.workQueue  workQueue;this.keepAliveTime  unit.toNanos(keepAliveTime);this.threadFactory  threadFactory;this.handler  handler;}参数1、corePoolSize核心线程数大小当线程数2、maximumPoolSize 最大线程数 当线程数 corePoolSize的时候会把runnable放入workQueue中3、keepAliveTime  保持存活时间当线程数大于corePoolSize的空闲线程能保持的最大时间。4、unit 时间单位5、workQueue 保存任务的阻塞队列6、threadFactory 创建线程的工厂7、handler 拒绝策略任务执行顺序1、当线程数小于corePoolSize时创建线程执行任务。2、当线程数大于等于corePoolSize并且workQueue没有满时放入workQueue中3、线程数大于等于corePoolSize并且当workQueue满时新任务新建线程运行线程总数要小于maximumPoolSize4、当线程总数等于maximumPoolSize并且workQueue满了的时候执行handler的rejectedExecution。也就是拒绝策略。ThreadPoolExecutor默认有四个拒绝策略1、ThreadPoolExecutor.AbortPolicy()   直接抛出异常RejectedExecutionException2、ThreadPoolExecutor.CallerRunsPolicy()    直接调用run方法并且阻塞执行3、ThreadPoolExecutor.DiscardPolicy()   直接丢弃后来的任务4、ThreadPoolExecutor.DiscardOldestPolicy()  丢弃在队列中队首的任务当然可以自己继承RejectedExecutionHandler来写拒绝策略.int corePoolSize  1;int maximumPoolSize  2;int keepAliveTime  10;//BlockingQueue workQueue  new LinkedBlockingQueue();BlockingQueue workQueue  new ArrayBlockingQueue(5);ThreadFactory threadFactory  Executors.defaultThreadFactory();//线程池和队列满了之后的处理方式//1.跑出异常RejectedExecutionHandler handler  new ThreadPoolExecutor.AbortPolicy();RejectedExecutionHandler handler2  new ThreadPoolExecutor.CallerRunsPolicy();RejectedExecutionHandler handler3  new ThreadPoolExecutor.DiscardPolicy();RejectedExecutionHandler handler4  new ThreadPoolExecutor.DiscardOldestPolicy();ThreadPoolExecutor threadPoolExecutor  new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler2);for (int j  1; j threadPoolExecutor.execute(new Runnable() {public void run() {try {System.out.println(Thread.currentThread().getName());TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}});}System.out.println(threadPoolExecutor);}
http://www.sadfv.cn/news/277784/

相关文章:

  • 网站会员体系方案软件网站关键词优化
  • 苏州市吴中区住房和城乡建设局官方网站个人网站能 做淘客吗
  • 大石桥城乡规划建设局网站温州网络学堂
  • 网站建设与管理课程实训为什么要建设旅游网站
  • 太原网站开发团队长岭网站优化公司
  • 做电商要关注哪些网站中国建筑集团排名
  • 网站建设服务哪家便宜网站用哪个做
  • iis6无法新建网站教资报名网站设置
  • 织梦网站怎么做索引地图0基础做网站工具
  • 网站建设属于淘宝哪种类目域名怎么起
  • 付给招聘网站的费用怎么做分录开封网站建设培训班
  • 机关网站建设 方案网上国网app推广
  • 中迅做网站是模板站吗新闻媒体发稿平台
  • 上海微信网站建设公司电话公司网站建设费属于什么费用
  • 网站建设纟金手指下拉壹陆wordpress升级后怎么设置字体颜色
  • 电子商务网站建设与维护李建忠下载企业网站推广技术
  • 什么网站可以做拍a发布会搜索引擎营销的主要方式有
  • 苏州高端网站建设定制手机模板的网站
  • 先建网站还是先做网页网站建设合同的结构
  • 南京高端网站建设做购物网站的图标从哪里来
  • php5 mysql网站开发基础与应用门户网站app
  • 黑色装修网站源码dede怎样入驻微信小程序
  • 中学生怎么做网站wordpress 文章之显示标题
  • 南昌网站小程序开发企业网站建设条件
  • 有发展的小企业网站建设怎样做百度网站推广
  • 网站规划与设计就业vs 2008 建立网站
  • 网站申请免费astro wordpress
  • 专业做国际网站wordpress trego
  • 电子科技网站永定路网站建设
  • 蓝奏云注册网站如何查看网站蜘蛛