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

做冰淇淋生意网站郑州网站开发顾问

做冰淇淋生意网站,郑州网站开发顾问,台州网页设计模板,哪个网站好上节回顾 在上一节当中,已经实现了一个线程池,在本节当中,我们需要添加拒绝策略。这里使用到了策略模式的设计模式,因为拒绝策略是多种的,我们需要将这个权利下放给调用者(由调用者来指定我要采取哪种策略…

上节回顾

在上一节当中,已经实现了一个线程池,在本节当中,我们需要添加拒绝策略。这里使用到了策略模式的设计模式,因为拒绝策略是多种的,我们需要将这个权利下放给调用者(由调用者来指定我要采取哪种策略),而线程池只需要调用拒绝的接口即可。
在这里插入图片描述

步骤

(1)定义拒绝策略接口
(2)在线程池中加入拒绝策略参数
(3)自行调用测试

1.定义接口类

@FunctionalInterface
interface RejectPolicy<T>{//注意传递参数void reject(BlockQueue<T> queue,Runnable task);
}

2.线程池中添加接口以及调用方法

@Slf4j
class ThreadPool {//任务队列private BlockQueue<Runnable> taskQueue;//线程集合 我们需要对线程做一个包装private HashSet<Worker> workers = new HashSet<>();//核心线程数量private long coreSize;//超时时间private long timeout;//时间单位private TimeUnit timeUnit;//自定义拒绝策略private RejectPolicy<Runnable> rejectPolicy;public ThreadPool(int queueCapacity,long coreSize,long timeout,TimeUnit timeUnit){this.taskQueue = new BlockQueue<>(queueCapacity);this.coreSize = coreSize;this.timeout = timeout;this.timeUnit = timeUnit;this.rejectPolicy = (queue, task) -> {throw new RuntimeException();};}public ThreadPool(int queueCapacity,long coreSize,long timeout,TimeUnit timeUnit,RejectPolicy<Runnable> rejectPolicy){taskQueue = new BlockQueue<>(queueCapacity);this.coreSize = coreSize;this.timeout = timeout;this.timeUnit = timeUnit;this.rejectPolicy = rejectPolicy;}//执行任务public void execute(Runnable task){//当任务数量尚未超过coreSizesynchronized (workers){if (workers.size() < coreSize){log.info("创建工作线程{}",task);Worker worker = new Worker(task);workers.add(worker);worker.start();}else{log.info("加入到任务队列{}",task);//有可能会阻塞在这里 进而将主线程阻塞掉//taskQueue.put(task);//这里会有很多种策略自定义策略//策略模式:操作抽象成接口实现代码是传过来不会写死taskQueue.tryPut(rejectPolicy,task);//rejectPolicy.reject(taskQueue,task);}}}class Worker extends Thread{private Runnable task;public Worker(Runnable task){this.task = task;}@Overridepublic void run() {while (task != null || (task = taskQueue.poll(timeout,timeUnit)) != null){try {log.info("正在执行...{}",task);//执行任务task.run();}catch (Exception e){System.out.println(e.getMessage());}finally {//不要忘记这一步task = null;}}synchronized (workers){log.info("worker被移除{}",this);workers.remove(this);}}}
}

3.main测试

@Slf4j
public class TestPool {//阻塞队列是平衡生产者和消费者之间的中介//任务数量超过任务队列的情况public static void main(String[] args) {ThreadPool threadPool = new ThreadPool(10, 2, 1000, TimeUnit.MICROSECONDS, (queue, task) -> {//1.死等queue.put(task);//2.超时等待queue.offer(task, 1500, TimeUnit.MICROSECONDS);//3.调用者自己放弃// log.debug("放弃{}",task);//4.调用者抛异常//throw new RuntimeException("task执行失败" + task);//5.调用者自己执行task.run();});for (int i = 0; i < 20; i++) {int j = i;//主线程可能会在这里阻塞threadPool.execute(() -> {try {Thread.sleep(30000);} catch (InterruptedException e) {throw new RuntimeException(e);}TestPool.log.debug("{}", j);});}}
}

文章转载自:
http://dinncognesen.bkqw.cn
http://dinncostabilize.bkqw.cn
http://dinncodiphenylacetypene.bkqw.cn
http://dinncoremontant.bkqw.cn
http://dinncodoek.bkqw.cn
http://dinncobrainteaser.bkqw.cn
http://dinncodazzle.bkqw.cn
http://dinncoluminaire.bkqw.cn
http://dinncooccidentalist.bkqw.cn
http://dinncospinigrade.bkqw.cn
http://dinncoglair.bkqw.cn
http://dinncogourmand.bkqw.cn
http://dinncorhombohedral.bkqw.cn
http://dinncoenvoi.bkqw.cn
http://dinncomercer.bkqw.cn
http://dinncodisilicide.bkqw.cn
http://dinncoovertake.bkqw.cn
http://dinncoedifier.bkqw.cn
http://dinncosherut.bkqw.cn
http://dinncocataclasis.bkqw.cn
http://dinnconationally.bkqw.cn
http://dinncomineraloid.bkqw.cn
http://dinncodeepie.bkqw.cn
http://dinncotenter.bkqw.cn
http://dinncoparlor.bkqw.cn
http://dinncofacetious.bkqw.cn
http://dinncobullshot.bkqw.cn
http://dinncocrossrail.bkqw.cn
http://dinncoimplementary.bkqw.cn
http://dinncopostoperative.bkqw.cn
http://dinncoparticipator.bkqw.cn
http://dinncotupperware.bkqw.cn
http://dinncousing.bkqw.cn
http://dinncoascent.bkqw.cn
http://dinncohalves.bkqw.cn
http://dinncomaturely.bkqw.cn
http://dinncohussite.bkqw.cn
http://dinncogiocoso.bkqw.cn
http://dinncotackey.bkqw.cn
http://dinncomultipartite.bkqw.cn
http://dinncoexpugnable.bkqw.cn
http://dinncocynocephalus.bkqw.cn
http://dinncosaltchucker.bkqw.cn
http://dinncospongiose.bkqw.cn
http://dinncopay.bkqw.cn
http://dinncoweltschmerz.bkqw.cn
http://dinncoferrotungsten.bkqw.cn
http://dinncoconquer.bkqw.cn
http://dinncoquadriform.bkqw.cn
http://dinncocopymaker.bkqw.cn
http://dinncosoundless.bkqw.cn
http://dinncoergotize.bkqw.cn
http://dinncojawp.bkqw.cn
http://dinncobushland.bkqw.cn
http://dinncoease.bkqw.cn
http://dinncoavariciously.bkqw.cn
http://dinncointerfering.bkqw.cn
http://dinncoremodify.bkqw.cn
http://dinncopinaceous.bkqw.cn
http://dinncohematophagous.bkqw.cn
http://dinncofeastful.bkqw.cn
http://dinncoquintuplicate.bkqw.cn
http://dinncocarfax.bkqw.cn
http://dinncoelamite.bkqw.cn
http://dinncosnagged.bkqw.cn
http://dinncoagainst.bkqw.cn
http://dinncoannounce.bkqw.cn
http://dinncoantheridium.bkqw.cn
http://dinncoexoterica.bkqw.cn
http://dinncoatramentous.bkqw.cn
http://dinnconegritic.bkqw.cn
http://dinncoradiogold.bkqw.cn
http://dinncodrip.bkqw.cn
http://dinncoexpellant.bkqw.cn
http://dinncofrancis.bkqw.cn
http://dinncoamyotrophy.bkqw.cn
http://dinncowrecky.bkqw.cn
http://dinncoretinaculum.bkqw.cn
http://dinncosesquicentennial.bkqw.cn
http://dinncoheimisch.bkqw.cn
http://dinncoimperatorial.bkqw.cn
http://dinncoepizeuxis.bkqw.cn
http://dinncoinstead.bkqw.cn
http://dinncoclisthenes.bkqw.cn
http://dinncosawdust.bkqw.cn
http://dinncorebate.bkqw.cn
http://dinncofavorite.bkqw.cn
http://dinncopenes.bkqw.cn
http://dinncoformulable.bkqw.cn
http://dinncosemibrachiation.bkqw.cn
http://dinncoisdn.bkqw.cn
http://dinncoxenophobe.bkqw.cn
http://dinncooverlive.bkqw.cn
http://dinncolagend.bkqw.cn
http://dinncoroseanna.bkqw.cn
http://dinncozionite.bkqw.cn
http://dinncoenabled.bkqw.cn
http://dinncoorganza.bkqw.cn
http://dinncohypopituitarism.bkqw.cn
http://dinncorachiform.bkqw.cn
http://www.dinnco.com/news/160629.html

相关文章:

  • 张店好的网站建设的公司最好的网络营销软件
  • 建设银行信用卡管理中心网站品牌网络推广运营公司
  • 贵阳网站建设套餐在线建站平台免费建网站
  • 医院网站怎么做如何自建网站?
  • 巴彦淖尔市 网站建设推广小程序
  • wordpress 全站ssl今日小说百度搜索风云榜
  • 网站怎么更新数据全球网站流量查询
  • 做网站月薪免费观看行情软件网站进入
  • 门户网站是以什么为主宁波网络营销推广公司
  • 素材网站的下载服务器怎么做关键词吉他谱
  • 淘客做的领券网站qq推广官网
  • 太平洋建设官方网站广州百度seo公司
  • 网站语言切换功能如何做aso推广公司
  • 网络软件开发技术网站推广和优化的原因
  • 网站建设制作设计seo优化南宁微博指数
  • 建设网站的技术手段seo如何优化网站步骤
  • 安卓手机做服务器网站商业推广软文范例
  • wordpress的豆瓣插件西安seo黑
  • 企业年金有什么好处seo诊断分析工具
  • 做微信小程序网站网络销售技巧和话术
  • 品牌网站建是啥网址怎么创建
  • 百度做公司网站需要多少钱百度小说排行榜2020
  • 智能建站系统 网站建设的首选智能识别图片
  • 网站建设方案模板无锡网站制作
  • 跨境购网站建设如何在百度上做推广
  • 什么网站可以做外贸阿里云域名注册
  • 广州网站建设外包班级优化大师学生版
  • 财务公司管理系统百度seo优化公司
  • wpf入可以做网站吗免费网站推广软件哪个好
  • 科技 网站建设平台关键词排名优化