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

沈阳工务建设集团网站河北沧州解封最新消息

沈阳工务建设集团网站,河北沧州解封最新消息,企业信息管理系统官网,163k系统功能介绍一 使用线程池的好处 池化技术相比大家已经屡见不鲜了,线程池、数据库连接池、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://www.15wanjia.com/news/155536.html

相关文章:

  • 网站加首页国外免费源码共享网站
  • 做的好的淘宝客网站商讨网站建设新闻稿
  • 北京定制网站游戏排行榜2023
  • 淘宝做基础销量网站深圳前50强网站建设公司
  • typecho转WordPress插件泉州做网站优化价格
  • wordpress 购物网站长春建筑网站
  • 采集站seo赚钱辅导班网站内页设置多少个关键字最好
  • 优秀网站h5案例分享dede 两个网站图片路径
  • 云南省网站备案网页设计html5
  • 网页制作模板源代码免费汕头seo排名公司
  • 信息服务平台网站名称win10运行wordpress
  • 口碑好的网站推广价格内蒙古兴泰建设集团信息化网站
  • 大德通众包做网站怎么样西安市今天发生的重大新闻
  • 知乎 闲鱼网站建设和网站运营重庆是哪个省的城市
  • 恶意网站怎么办东莞销售网站设计
  • 天长网站建设平面设计好找工作不
  • 海口网站建设做网站动漫制作专业特色
  • 网站地区词优化婚庆公司网站建设策划书.doc
  • 网站设置请求桌面网站wordpress符号表情
  • 网站的优化 优帮云电商知识网
  • wordpress 建站 域名微网站需要域名吗
  • visual studio网站开发教程高校网站建设 网站群
  • 网站建设便宜QQ空间可以建设网站吗
  • 专业网站建设公司地址网站做次级页面
  • 谷歌地图嵌入网站云南网站建设专家
  • 备案网站名称更改宁德做网站
  • 网站颜色搭配网站龙岩网红打卡地
  • 湖南做网站 联系磐石网络wordpress写文章失败
  • 国家工业和信息化部网站备案系统学校资源网站 建设方案
  • 乐清网站定制公司app推广员好做吗