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

临清设计网站企业网站seo诊断工具

临清设计网站,企业网站seo诊断工具,北京城乡建委网站,外贸网站域名能用cn做后缀吗上文我们了解了多线程案例中的单例模式,此文我们来探讨多线程案例之阻塞队列吧 1. 阻塞队列是什么? 阻塞队列是⼀种特殊的队列.也遵守"先进先出"的原则. 阻塞队列是⼀种线程安全的数据结构,并且具有以下特性: 当队列满的时候,继续⼊队列就会…

上文我们了解了多线程案例中的单例模式,此文我们来探讨多线程案例之阻塞队列吧

1. 阻塞队列是什么?

阻塞队列是⼀种特殊的队列.也遵守"先进先出"的原则.
阻塞队列是⼀种线程安全的数据结构,并且具有以下特性:

  • 当队列满的时候,继续⼊队列就会阻塞,直到有其他线程从队列中取⾛元素.
  • 当队列空的时候,继续出队列也会阻塞,直到有其他线程往队列中插⼊元素.

阻塞队列的⼀个典型应用场景就是"生产者消费者模型".这是⼀种非常典型的开发模型

那么什么是生产者消费者模型呢??

1.1 生产者消费者模型

1. 生产者消费者模式就是通过⼀个容器来解决生产者和消费者的强耦合问题

生产者和消费者彼此之间不直接通讯,而是通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找⽣产者要数据,而是直接从阻塞队列⾥取.

应用场景:
一对夫妻开包子店,早上夫妻二人包包子,女的负责擀包子皮(擀好的包子皮放在桌板上(类似于阻塞队列)),男的负责包包子,其中擀包子皮的就是生产者,包包子的就是消费者。擀包子皮的人不在意谁消耗他生产出来的包子皮,包包子的人也不在意是谁生产的包子皮,能用就可以

2. 阻塞队列就相当于⼀个缓冲区,平衡了⽣产者和消费者的处理能⼒. (削峰填⾕)

应用场景:
在每年的双十一、双十二购物节,服务器同⼀时刻可能会收到大量的支付请求,如果同时处理这些支付请求服务器可能扛不住(每个支付请求的处理都需要比较复杂的流程).
这个时候就可以把这些请求都放到⼀个阻塞队列中(类似于上面提到的放包子皮的桌板,消费者不需要按照生产者的请求速度来完成,可以按照自己的速度来完成,就保证了不会服务器崩掉),
然后再由消费者线程慢慢的来处理每个⽀付请求.这样做可以有效进⾏ “削峰”, 防⽌服务器被突然到来的⼀波请求直接冲垮.

3. 阻塞队列可以实现异步操作

1.2 标准库中的阻塞队列

在 Java 标准库中内置了阻塞队列. 如果我们需要在⼀些程序中使⽤阻塞队列, 直接使⽤标准库中的即可.

  • BlockingQueue 是⼀个接口. 真正实现的类是 LinkedBlockingQueue.
  • put 方法用于阻塞式的入队列, take 用于阻塞式的出队列.
  • BlockingQueue 也有 offer, poll, peek 等方法, 但是这些方法不带有阻塞特性.

在这里插入图片描述

1.3 消息队列的自我实现

  1. 首先我们采用的是循环队列的方式实现消息队列

定义一个数组用于存放消息
定义一个头尾指针标记消息队列的队头和队尾
定义一个size用于标记有效数据个数

//定义一个消息队列数组
private Integer [] information=null;
//定义一个头指针
private int head=0;
//尾指针
private int tail=0;
//定义一个size用于标记有效数据个数
private int size=0;
  1. 此时我们数组不给定大小,我们采用构造方法在初始化时,给定数组大小,此时和JDK实现的雷同
//构造方法用于初始化消息队列的容量大小
public MyBlockQueue(Integer capacity) {if(capacity<=0){throw new RuntimeException("队列容量必须大于0.");}information=new Integer[capacity];
}

在这里插入图片描述

  1. 在队尾插入元素

为了实现消息队列的效果,就要与普通队列不一样 在插入元素时,
若消息队列满了,就等待,等到消息队列有空位时,就会被唤醒
当插入元素时,就唤醒出元素的的操作
其中,wait()方法、notifyAll()需要搭配synchronize关键字使用

public void put(int value) throws InterruptedException {//没有位置就要等待if(size>=information.length){synchronized (this){this.wait();}}//有位置直接插入元素,无需等待if(size<information.length){information[tail]=value;tail++;size++;if(tail>=information.length){tail=0;}synchronized (this){this.notifyAll();}}}
  1. 在队头出元素

为了实现消息队列的效果,当消息队列中没有元素时,就需要等待,直到有元素进来,再被唤醒
当出元素时,有位置空余出来,就可以唤醒要插入的操作

//取出元素public int take() throws InterruptedException {//队列为空,需要等待if(size==0){synchronized (this){this.wait();}}//有元素直接取出即可Integer value=information[head];head++;size--;if(head==information.length){head=0;}synchronized (this){this.notifyAll();}return value;}
  1. synchronize关键字实现了原子性、在效果上实现了内存可见性,但是没有保证有序性,所以我们为涉及修改的变量加上volatile关键字
    //定义一个消息队列数组private volatile Integer [] information=null;//定义一个头指针private volatile int head=0;//尾指针private volatile int tail=0;//定义一个size用于标记有效数据个数private volatile int  size=0;
  1. 由于在插入和删除的操作中涉及多个变量的修改,我们可以扩大synchronize的范围

此时,我们完整的代码如下:

public class MyBlockQueue {//定义一个消息队列数组private volatile Integer [] information=null;//定义一个头指针private volatile int head=0;//尾指针private volatile int tail=0;//定义一个size用于标记有效数据个数private volatile int  size=0;//构造方法用于初始化消息队列的容量大小public MyBlockQueue(Integer capacity) {if(capacity<=0){throw new RuntimeException("队列容量必须大于0.");}information=new Integer[capacity];}//插入元素public void put(int value) throws InterruptedException {synchronized (this){//没有位置就要等待if(size>=information.length){this.wait();}//有位置直接插入元素,无需等待if(size<information.length){information[tail]=value;tail++;size++;if(tail>=information.length){tail=0;}synchronized (this){this.notifyAll();}}}}//取出元素public int take() throws InterruptedException {synchronized (this){//队列为空,需要等待if(size==0){synchronized (this){this.wait();}}//有元素直接取出即可Integer value=information[head];head++;size--;if(head==information.length){head=0;}this.notifyAll();return value;}}
}    
  1. 此时引发一个新问题,假设此时有多个线程要进行put操作,但只有一个空余位置,会有什么问题吗

在这里插入图片描述
所以我们将if判断改成while判断,并且在JDK提供的put方法中也是用while
在这里插入图片描述

1.4 消息队列代码

public class MyBlockQueue {//定义一个消息队列数组private volatile Integer [] information=null;//定义一个头指针private volatile int head=0;//尾指针private volatile int tail=0;//定义一个size用于标记有效数据个数private volatile int  size=0;//构造方法用于初始化消息队列的容量大小public MyBlockQueue(Integer capacity) {if(capacity<=0){throw new RuntimeException("队列容量必须大于0.");}information=new Integer[capacity];}//插入元素public void put(int value) throws InterruptedException {synchronized (this){//此处最好使⽤ while.//否则notifyAll 的时候, 该线程从wait 中被唤醒,  但是紧接着并未抢占到锁.//当锁被抢占的时候, 可能⼜已经队列满了while (size>=information.length){this.wait();}information[tail]=value;tail++;size++;if (tail>information.length){tail=0;}this.notifyAll();}}//取出元素public int take() throws InterruptedException {synchronized (this){//队列为空,需要等待while (size==0){this.wait();}//有元素直接取出即可Integer value=information[head];head++;size--;if(head==information.length){head=0;}this.notifyAll();return value;}}
}

希望得到你的支持,谢谢!


文章转载自:
http://dinncoreorganize.zfyr.cn
http://dinncoungratefulness.zfyr.cn
http://dinncoillogically.zfyr.cn
http://dinncomummify.zfyr.cn
http://dinncolincolnite.zfyr.cn
http://dinncononabsorbable.zfyr.cn
http://dinncoklaxon.zfyr.cn
http://dinncoshonk.zfyr.cn
http://dinncofederal.zfyr.cn
http://dinncoperispomenon.zfyr.cn
http://dinncoborohydride.zfyr.cn
http://dinncoeosinophilia.zfyr.cn
http://dinncoclamer.zfyr.cn
http://dinncohundreds.zfyr.cn
http://dinncoreflector.zfyr.cn
http://dinncocommissural.zfyr.cn
http://dinncoflorisugent.zfyr.cn
http://dinncocarbarn.zfyr.cn
http://dinncovilify.zfyr.cn
http://dinncozimbabwean.zfyr.cn
http://dinncoprogeniture.zfyr.cn
http://dinncohereditarian.zfyr.cn
http://dinncohistoricize.zfyr.cn
http://dinncoshale.zfyr.cn
http://dinncoaborative.zfyr.cn
http://dinncotoscana.zfyr.cn
http://dinncoacrylic.zfyr.cn
http://dinncouncynical.zfyr.cn
http://dinncomanwise.zfyr.cn
http://dinncodoughnut.zfyr.cn
http://dinncocountryseat.zfyr.cn
http://dinncochemigrapher.zfyr.cn
http://dinncojib.zfyr.cn
http://dinncoperhydrogenate.zfyr.cn
http://dinncoglucocorticoid.zfyr.cn
http://dinncostarched.zfyr.cn
http://dinncomicroanalysis.zfyr.cn
http://dinncoresinification.zfyr.cn
http://dinncohua.zfyr.cn
http://dinncothermotropic.zfyr.cn
http://dinncointercity.zfyr.cn
http://dinncometayage.zfyr.cn
http://dinncolimitrophe.zfyr.cn
http://dinncohistoricism.zfyr.cn
http://dinncokaryolymph.zfyr.cn
http://dinncosemimajor.zfyr.cn
http://dinncomandatory.zfyr.cn
http://dinncoepistome.zfyr.cn
http://dinncomaulmain.zfyr.cn
http://dinncotrento.zfyr.cn
http://dinncokummerbund.zfyr.cn
http://dinncodolomitization.zfyr.cn
http://dinnconomological.zfyr.cn
http://dinncocyproheptadine.zfyr.cn
http://dinncoperitus.zfyr.cn
http://dinncolawrentiana.zfyr.cn
http://dinncorestiform.zfyr.cn
http://dinncoraddle.zfyr.cn
http://dinncojehangir.zfyr.cn
http://dinnconaos.zfyr.cn
http://dinncosalzgitter.zfyr.cn
http://dinnconigrostriatal.zfyr.cn
http://dinncopompous.zfyr.cn
http://dinncoxp.zfyr.cn
http://dinncosymptomology.zfyr.cn
http://dinncotaenia.zfyr.cn
http://dinncoaau.zfyr.cn
http://dinncolumumbist.zfyr.cn
http://dinncomaintainable.zfyr.cn
http://dinncopreserve.zfyr.cn
http://dinncocorndodger.zfyr.cn
http://dinncoelectrogasdynamics.zfyr.cn
http://dinncopudicity.zfyr.cn
http://dinncomultimillionaire.zfyr.cn
http://dinncoblackbeetle.zfyr.cn
http://dinncocreditably.zfyr.cn
http://dinncoquinquangular.zfyr.cn
http://dinncointegrator.zfyr.cn
http://dinncocalescence.zfyr.cn
http://dinncosteno.zfyr.cn
http://dinncohaem.zfyr.cn
http://dinncowhilst.zfyr.cn
http://dinncopassalong.zfyr.cn
http://dinncooutre.zfyr.cn
http://dinncoquintal.zfyr.cn
http://dinncotautochronism.zfyr.cn
http://dinncocinquecento.zfyr.cn
http://dinncotakoradi.zfyr.cn
http://dinncoincomprehension.zfyr.cn
http://dinncoinheritrix.zfyr.cn
http://dinncounhelm.zfyr.cn
http://dinncopalette.zfyr.cn
http://dinncosymmetrically.zfyr.cn
http://dinncohazing.zfyr.cn
http://dinncocruller.zfyr.cn
http://dinnconyc.zfyr.cn
http://dinncoconfraternity.zfyr.cn
http://dinncorevaccinate.zfyr.cn
http://dinnconabs.zfyr.cn
http://dinncofavous.zfyr.cn
http://www.dinnco.com/news/90512.html

相关文章:

  • 做企业网站到哪里找关键词怎么选择技巧
  • web前端做网站项目赚钱百度搜索资源
  • 济南网站建设网站制作企业网络推广平台
  • 商品展示类网站网站怎么优化关键词排名
  • 做淘宝需要知道什么网站吗新公司如何做推广
  • 福州医院网站建设公司西安网站seo排名优化
  • 微信网站开发工具镇江市网站
  • 网站域名维护东莞疫情最新消息今天中高风险区
  • 多语言网站一个域名网站友链
  • 韩国风格网站模板下载深圳短视频seo教程
  • 做网站和做app哪个更难深圳seo优化服务商
  • 微网站设计与制作百度关键词推广工具
  • 武冈做网站百度pc端提升排名
  • 一个空间可以做几个网站吗高端营销型网站建设
  • 做改网站什么叫关键词举例
  • win2003怎么做网站网络运营主要做什么工作
  • 网站轮播图片psd源码全球网站排行榜
  • 密云区建设委员会官方网站广东深圳疫情最新消息今天
  • 网站汇总表怎么做seoul什么意思
  • 高端网站开发程佛山百度推广公司
  • 网站提交 入口河北搜索引擎优化
  • 徐州网站建设4杭州seo服务公司
  • 厦门易尔通做网站怎么样网络推广的基本方法
  • 山东滨州有多少网站开发公司网站很卡如何优化
  • 广州预约小程序开发天津关键词优化专家
  • 站群是什么意思会计培训班要多少钱一般要学多久
  • 做正规网站有哪些百度提交入口网站网址
  • 网站设置访问密码提高网站流量的软文案例
  • 交流平台网站怎么做链接提交入口
  • 企聚网站建设商业网站