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

企业网站建设专业服务广州官方网站建设

企业网站建设专业服务,广州官方网站建设,产品网上推广的方法有哪些,免费的html代码模板什么是阻塞队列 相比于一般的队列#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.zqtcl.cn/news/441616/

相关文章:

  • 老虎淘客系统可以做网站吗绍兴网站专业制作
  • 小公司做网站需要 align center
  • 自己做的网站怎么上传网络什么网站可以找人做系统
  • 公司在兰州要做网站怎样选择做期货关注什么网站
  • 响应式网站是指自适应吗新开传奇网站首区
  • 做网站产品介绍wordpress 参数 传递
  • 网站颜色搭配技巧建设摩托车价格大全
  • 哪些网站可以做画赚钱宁波模板建站定制
  • 昆明门户网站建设wordpress权限设置
  • 设计网站建设图片wordpress博客置顶
  • 网站上海网站建设网站数据建设涉及哪些内容
  • 3d效果图教程网站宁波网站建设慕枫科技
  • 视频结交网站怎么做想创建一个网站
  • 电商网站销售数据分析上海企业信息登记号查询
  • 网站建设规划设计公司排名无锡网站建设 君通科技
  • 徐州网站开发要多少钱给个人网站做百度百科
  • 法语网站建设免费网站为何收录比较慢
  • 品牌网站推广软件seo内链优化
  • 广东律师事务所东莞网站建设做网站 怎么备案
  • shopnc本地生活o2o网站源码wordpress文章内多页效果
  • 深圳全国网站制作哪个好页面设计有哪几种风格
  • 网页设计作业网站素材和效果图夏天做啥网站致富
  • 利用帝国软件如何做网站网站友链交换平台
  • 简述网站开发技术深圳网站设计合理刻
  • wordpress网站名称寻找销售团队外包
  • 一浪网站建设网页qq邮箱
  • 做网站需要注册公司吗夫唯seo系统培训
  • 沈阳人流哪个医院好安全百度关键词优化怎么做
  • 1688网站怎么做分销高质量的网站内容建设
  • 网站建设公司济宁网站转跳怎么做