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

想自己建个网站徐州做网站软件

想自己建个网站,徐州做网站软件,只做水果的网站,网站后期推广是谁来做什么是阻塞队列 相比于一般的队列#xff0c;有两个特点 1.线程安全 2.带有阻塞功能 1#xff09;队伍为空时#xff0c;出队列就会出现阻塞#xff0c;阻塞到其他线程入队列为止 2#xff09;队伍为满时#xff0c;入队列就会出现阻塞#xff0c;阻塞到其他线程出队列…什么是阻塞队列 相比于一般的队列有两个特点 1.线程安全 2.带有阻塞功能 1队伍为空时出队列就会出现阻塞阻塞到其他线程入队列为止 2队伍为满时入队列就会出现阻塞阻塞到其他线程出队列为止 常用于生产者消费者模型 作用 1.解耦合 2.削峰填谷 使用阻塞队列 public class Test12 {public static void main(String[] args) throws InterruptedException {BlockingQueueString queue new ArrayBlockingQueue(100);queue.put(qqq);String elem queue.take();System.out.println(elem elem);elem queue.take();System.out.println(elem elem);} } 运行结果 不会结束运行一直在等待。 使用put和offer一样的都是入队列但是put是带有阻塞功能offer没有带阻塞功能队满了就会返回结果 take方法用来出队列也是带有阻塞功能 实现阻塞队列 1实现普通队列 class MyBlockingQueue{private String[] elems null;private int size 0;private int head 0;private int tail 0;public MyBlockingQueue(int capacity){elems new String[capacity];}public void put(String elem) throws InterruptedException {if (size elems.length){//阻塞功能}elems[tail] elem;tail;if (tail elems.length){tail 0;}size;}public String take() throws InterruptedException {if (size 0){//阻塞功能}String elem null;elem elems[head];head;if (head elems.length){head 0;}size--;return elem;}}}2加上线程安全 class MyBlockingQueue{private String[] elems null;private int size 0;private int head 0;private int tail 0;private Object locker new Object();public MyBlockingQueue(int capacity){elems new String[capacity];}public void put(String elem) throws InterruptedException {synchronized(locker){if (size elems.length){}elems[tail] elem;tail;if (tail elems.length){tail 0;}size;}}public String take() throws InterruptedException {synchronized(locker){if (size 0){}String elem null;elem elems[head];head;if (head elems.length){head 0;}size--;return elem;}}}3加上阻塞功能 class MyBlockingQueue{private String[] elems null;private int size 0;private int head 0;private int tail 0;private Object locker new Object();public MyBlockingQueue(int capacity){elems new String[capacity];}public void put(String elem) throws InterruptedException {synchronized(locker){while (size elems.length){locker.wait();}elems[tail] elem;tail;if (tail elems.length){tail 0;}size;locker.notify();}}public String take() throws InterruptedException {synchronized(locker){while (size 0){locker.wait();}String elem null;elem elems[head];head;if (head elems.length){head 0;}size--;locker.notify();return elem;}}}代码解释 最终代码将if改成了while因为if只能判定一次如果出现以下情况就会出bug线程A线程B都执行到了put中的wait因为队列已满而停止运行线程C出队列唤醒了线程A线程A继续入队列入队列后就会notify导致唤醒了线程B而此时队列已满无法进行入队操作就出现了bug所以就使用whilewait之前判定一次唤醒之后再进行一次判定相当于多做一步确定操作 简单的生产者消费者模型 class MyBlockingQueue{private String[] elems null;private int size 0;private int head 0;private int tail 0;private Object locker new Object();public MyBlockingQueue(int capacity){elems new String[capacity];}public void put(String elem) throws InterruptedException {synchronized(locker){while (size elems.length){locker.wait();}elems[tail] elem;tail;if (tail elems.length){tail 0;}size;locker.notify();}}public String take() throws InterruptedException {synchronized(locker){while (size 0){locker.wait();}String elem null;elem elems[head];head;if (head elems.length){head 0;}size--;locker.notify();return elem;}}} public class Test11 {public static void main(String[] args) throws InterruptedException {MyBlockingQueue myBlockingQueue new MyBlockingQueue(100);Thread t1 new Thread(()-{int n 1;while(true){try {myBlockingQueue.put(n );System.out.println(生产元素 n);n;} catch (InterruptedException e) {throw new RuntimeException(e);}}});Thread t2 new Thread(()-{while(true){try {String n myBlockingQueue.take();System.out.println(消费元素 n);Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t1.start();t2.start();} }
http://www.pierceye.com/news/872322/

相关文章:

  • 蓝色系网站设计企业应对承包商的施工方案尤其是
  • 旅游网站 源码 织梦导购网站开发
  • 头像制作网站开源低代码平台
  • 网站到期域名怎么解决办法自己动手建立网站3
  • 比较有名的网站建设平台吉林建设网站
  • 网站服务器解决方案wamp安装wordpress
  • 义乌制作网站赣州网站建设公司
  • 东莞网站平台后缀建设淘宝客网站
  • 深圳龙华新区住房和建设局网站示范校建设专题网站
  • 成都制作网站的公司简介wordpress录入表单写数据库
  • 中山网站设计收费标准互联网保险发展现状和趋势
  • 公司网站发布流程简述企业网络建设的步骤
  • 哪些网站可以做问卷第1063章 自己做视频网站
  • 电子商务网站 费用做p2p网站
  • 网站建设 猴王网络厦门app开发网站开发公司电话
  • 做3d图的网站有哪些比wordpress更好的网站程序
  • 仿做网站可以整站下载器吧网络网站建设公司
  • 网站流量用完wordpress page 父页面
  • 旅游地网站制作有没有做网站的高手
  • 有什么网站可以做一起作业什么网站可以兼职做效果图
  • 工程中标查询网站长沙网站制作作
  • 免费网站下载直播软件企业品牌网站建设类型
  • 建立网站并以此为基础从事经营活动的企业称为什么免费销售网站模板
  • 成都市建设质监站网站微信企业网站html5模板
  • 福建工程建设管理中心网站仙桃做企业网站的
  • 孝感做网站的公司建网站是永久的吗
  • 厦门手机建站php网站开发推荐书籍
  • 属于c2c网站的有哪几个方庄网站制作
  • 建设局网站模板iis 网站没有上传权限
  • 建设网站龙华怎么用自己的电脑搭建网站