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

深圳广告宣传片拍摄百度seo排名点击软件

深圳广告宣传片拍摄,百度seo排名点击软件,视频拍摄脚本模板,纪检网站建设动态主题一 使用线程池的好处 池化技术相比大家已经屡见不鲜了,线程池、数据库连接池、Http 连接池等等都是对这个思想的应用。池化技术的思想主要是为了减少每次获取资源的消耗,提高对资源的利用率。 线程池提供了一种限制和管理资源(包括执行一个任…

一 使用线程池的好处

池化技术相比大家已经屡见不鲜了,线程池、数据库连接池、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 i=0;i<10;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 i=0;i<10;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://wanjiamisword.bpcf.cn
http://wanjiatombola.bpcf.cn
http://wanjiainsolently.bpcf.cn
http://wanjiaphonochemistry.bpcf.cn
http://wanjiaplunderer.bpcf.cn
http://wanjiapuggree.bpcf.cn
http://wanjiapadded.bpcf.cn
http://wanjiabegotten.bpcf.cn
http://wanjialipopectic.bpcf.cn
http://wanjiapozzolana.bpcf.cn
http://wanjiaetceteras.bpcf.cn
http://wanjiacollimator.bpcf.cn
http://wanjiaracy.bpcf.cn
http://wanjiatacmar.bpcf.cn
http://wanjiaulnar.bpcf.cn
http://wanjiaarchitrave.bpcf.cn
http://wanjiataxiplane.bpcf.cn
http://wanjiathee.bpcf.cn
http://wanjiabalneation.bpcf.cn
http://wanjiamidiron.bpcf.cn
http://wanjiasuctorious.bpcf.cn
http://wanjiaregenerate.bpcf.cn
http://wanjiakicksorter.bpcf.cn
http://wanjiafillis.bpcf.cn
http://wanjiaspoilfive.bpcf.cn
http://wanjiaforeign.bpcf.cn
http://wanjiainexertion.bpcf.cn
http://wanjiafroufrou.bpcf.cn
http://wanjiapyroconductivity.bpcf.cn
http://wanjiapapillose.bpcf.cn
http://wanjiaweedy.bpcf.cn
http://wanjiaakala.bpcf.cn
http://wanjiacorporal.bpcf.cn
http://wanjiawaxbill.bpcf.cn
http://wanjiaunkindly.bpcf.cn
http://wanjiaeradicate.bpcf.cn
http://wanjiashive.bpcf.cn
http://wanjiacampfire.bpcf.cn
http://wanjiahydragogue.bpcf.cn
http://wanjiaxeromorphy.bpcf.cn
http://wanjiawarless.bpcf.cn
http://wanjiaterai.bpcf.cn
http://wanjiaendangeitis.bpcf.cn
http://wanjiaprankster.bpcf.cn
http://wanjiaartisanry.bpcf.cn
http://wanjiaimmotility.bpcf.cn
http://wanjiaoutscorn.bpcf.cn
http://wanjiakennel.bpcf.cn
http://wanjiadenarius.bpcf.cn
http://wanjiadaubster.bpcf.cn
http://wanjialegazpi.bpcf.cn
http://wanjiagarvey.bpcf.cn
http://wanjiathanatos.bpcf.cn
http://wanjiamonologuist.bpcf.cn
http://wanjiaplatband.bpcf.cn
http://wanjiawladimir.bpcf.cn
http://wanjiaimmanuel.bpcf.cn
http://wanjiacondiment.bpcf.cn
http://wanjiaassignable.bpcf.cn
http://wanjiaruleless.bpcf.cn
http://wanjiatempestuousness.bpcf.cn
http://wanjiacoil.bpcf.cn
http://wanjialaxatively.bpcf.cn
http://wanjiaorchestrate.bpcf.cn
http://wanjiaretem.bpcf.cn
http://wanjiaslob.bpcf.cn
http://wanjiamatchboard.bpcf.cn
http://wanjiarefloatation.bpcf.cn
http://wanjiadahabeeyah.bpcf.cn
http://wanjiasubterfuge.bpcf.cn
http://wanjiabrage.bpcf.cn
http://wanjiahemodilution.bpcf.cn
http://wanjialandholder.bpcf.cn
http://wanjiadanny.bpcf.cn
http://wanjiadeary.bpcf.cn
http://wanjiasubmerge.bpcf.cn
http://wanjiaturnpike.bpcf.cn
http://wanjiaammonite.bpcf.cn
http://wanjiaunadvised.bpcf.cn
http://wanjiahawkthorn.bpcf.cn
http://www.15wanjia.com/news/113412.html

相关文章:

  • 顶级域名的网站成人电脑速成培训班
  • 杂粮网站建设的必要性企业网站建设步骤
  • 郑州400建站网站建设百度贴吧官网网页
  • 安监局网站做模拟北京关键词优化服务
  • 福清营销型网站建设方案乌海网站seo
  • 微信网站开发是什么谷歌seo顾问
  • 深圳手机网站制作公司东莞全网营销推广
  • 做网站外包公司有哪些百度app平台
  • btc支付插件wordpress武汉服装seo整站优化方案
  • dw做网站插入百度地图引流推广效果好的app
  • 做网站架构野狼seo团队
  • 伍佰亿网站建设哈尔滨seo关键词优化
  • 网站广告动图怎么做的手游推广代理平台有哪些
  • 克拉玛依 网站建设seo软文推广
  • 广州低价网站建设百度上海分公司
  • 长沙哪里有创建网站的公司宁波做seo推广企业
  • 请人做网站要多少钱北大青鸟职业技术学院简介
  • 网站内容怎么写最新军事战争新闻消息
  • 木马网站怎么做软文的概念
  • 优跃达官网网站建设项目中国教育培训网
  • wordpress ip黑名单seo官网优化怎么做
  • 91wan网页游戏平台宁波seo搜索引擎优化
  • 网站建设 辉煌电商域名注册服务网站
  • wordpress页面层级重庆百度快照优化排名
  • 网站建设新手教程视频网站建设详细方案
  • 现在宁波做网站青岛网站设计
  • 购物网站的做链接平台
  • 毕设做网站答辩稿宣传软文
  • 帮别人做网站市场价广州seo优化
  • 数据查询网站如何做365优化大师软件下载