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

php网站开发目录网站cms相关知识

php网站开发目录,网站cms相关知识,卖机械设备什么网站做推广好,电子行业网站建设上文中我们讲了Java库中自带的阻塞队列#xff0c;并且讲了如何用阻塞队列来实现生产者消费者模型 【Java】用Java库中自带的阻塞队列以及用阻塞队列实现生产者-消费者模型 下面我们来讲如何用代码实现一个阻塞队列 1、实现一个阻塞队列 阻塞队列 普通队列 线程安全 阻… 上文中我们讲了Java库中自带的阻塞队列并且讲了如何用阻塞队列来实现生产者消费者模型 【Java】用Java库中自带的阻塞队列以及用阻塞队列实现生产者-消费者模型 下面我们来讲如何用代码实现一个阻塞队列  1、实现一个阻塞队列 阻塞队列 普通队列 线程安全 阻塞 1首先实现一个普通队列 class MyBlockingQueue{private int head 0;private int tail 0;private int size 0;String[] array;public MyBlockingQueue(){array new String[1000];}//取出队首元素public String take() throws InterruptedException {//如果队列为空则返回nullif (size 0){return null;}//取出队首元素String elem array[head];//如果head已经到了队尾那么下一个置0if(head array.length){head 0;}head;size--;return elem;}//放入元素public void put(String elem) throws InterruptedException { if (size array.length){return;}array[tail] elem;if (tail array.length){tail 0;}tail;size;} } 2线程安全  由于put()和take()方法中对各个变量都进行了多次修改因此我们在实现线程安全时直接对这两段代码加锁 public String take() throws InterruptedException {synchronized{if (size 0){return null;}String elem array[head];if(head array.length){head 0;}head;size--;return elem;}} public void put(String elem) throws InterruptedException { synchronized{if (size array.length){return;}array[tail] elem;if (tail array.length){tail 0;}tail;size;}} 并且为了防止内存可见性问题和指令重排序问题我们给三个变量加上volatile关键字进行修饰 什么是可见性问题和指令重排序问题 【Java】volatile-内存可见性问题 【Java】多线程-单例模式/volatile-指令重排序  private volatile int head 0; private volatile int tail 0; private volatile int size 0; 3阻塞 最后再加上阻塞 取队首元素时如果队列为空那么我们直接进行阻塞等到下一次在另一个线程放入元素时将其唤醒 放元素时如果队列满了我们将这个线程阻塞等到队列可用时我们在另一个线程唤醒 public String take() throws InterruptedException {synchronized (this){if (size 0){this.wait();}String elem array[head];if(head array.length){head 0;}head;size--;this.notify();return elem;}}public void put(String elem) throws InterruptedException {synchronized (this){if (size array.length){this.wait();}array[tail] elem;if (tail array.length){tail 0;}tail;size;this.notify();}}注意他们唤醒的对应关系 4while循环 这其中还存在一个问题那就是wait()的对象只能被notify()唤醒吗 答案是不。除了用notify()唤醒发生InterruptedException异常也可以将对象唤醒 假设队列为空的情况下发生了InterruptedException异常对象被唤醒代码继续往下执行再想取元素便会出错。因此这种情况下我们还要继续判断队列是否为空 为了解决这个问题我们将if判断改为while()循环判断就可以避免上面情况发生 //取出队首元素public String take() throws InterruptedException {synchronized (this){while (size 0){this.wait();}String elem array[head];if(head array.length){head 0;}head;size--;this.notify();return elem;}}//放入元素public void put(String elem) throws InterruptedException {synchronized (this){//判断队列是否满了如果满了则阻塞while (size array.length){this.wait();}array[tail] elem;if (tail array.length){tail 0;}tail;size;this.notify();}} 5完整代码 实现阻塞队列的完整代码如下 class MyBlockingQueue{private volatile int head 0;private volatile int tail 0;private volatile int size 0;String[] array;public MyBlockingQueue(){array new String[1000];}//取出队首元素public String take() throws InterruptedException {synchronized (this){while (size 0){this.wait();}String elem array[head];if(head array.length){head 0;}head;size--;this.notify();return elem;}}//放入元素public void put(String elem) throws InterruptedException {synchronized (this){//判断队列是否满了如果满了则阻塞while (size array.length){this.wait();}array[tail] elem;if (tail array.length){tail 0;}tail;size;this.notify();}} } 2、实现生产者-消费者模型 代码如下 class MyBlockingQueue{private volatile int head 0;private volatile int tail 0;private volatile int size 0;String[] array;public MyBlockingQueue(){array new String[1000];}//取出队首元素public String take() throws InterruptedException {synchronized (this){while (size 0){this.wait();}String elem array[head];if(head array.length){head 0;}head;size--;this.notify();return elem;}}//放入元素public void put(String elem) throws InterruptedException {synchronized (this){//判断队列是否满了如果满了则阻塞while (size array.length){this.wait();}array[tail] elem;if (tail array.length){tail 0;}tail;size;this.notify();}} }public class demo2 {public static void main(String[] args) {MyBlockingQueue myBlockingQueue new MyBlockingQueue();//生产者Thread thread1 new Thread(()-{int n 0;while (true){try {myBlockingQueue.put(n );} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(生产元素n);n;try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}}});//消费者Thread thread2 new Thread(()-{while (true){try {System.out.println(消费元素 myBlockingQueue.take());} catch (InterruptedException e) {throw new RuntimeException(e);}}});thread1.start();thread2.start();} }运行结果如图
http://www.zqtcl.cn/news/486872/

相关文章:

  • 网站如何备案工信局学网站开发首先学哪些基础
  • 什么网站利于优化河北省建设局网站材料备案
  • 自学装修设计从哪里入手沈阳百度seo
  • 做jsp网站用哪些软件下载如何利用网站赚钱
  • 注册网站域名需要什么湘潭公司做网站
  • 一个网站如何优化企业资质查询平台
  • 模板网站为什么做不了优化山西网络网站建设销售公司
  • 建设什么网站可以赚钱设计本网站是用什么做的
  • 荆州市网站建设策划师
  • 苏州中国建设银行招聘信息网站中国企业登记网
  • 网站服务器的重要性新闻软文范例大全
  • 茶叶网站建设一般的风格加大志愿服务网站建设
  • 湖州医院网站建设方案网页游戏知乎
  • 以网站建设为开题报告临海门户网站住房和城乡建设规划局
  • 河南省大型项目建设办公室网站wordpress置顶功能
  • 奉化网站建设三合一网站建设多少钱
  • wordpress文章页怎么调用网站图片wordpress菜单锚点定位
  • 网站建设运营合作合同网站建设英文合同
  • wordpress chrome插件开发图片式网站利于做优化吗
  • 如何做好品牌网站建设策划app要有网站做基础
  • 横沥网站建设公司wordpress运行php
  • 南皮网站建设价格网络推广这个工作好做吗
  • 长安大学门户网站是谁给做的网站排名logo怎么做
  • 襄樊做网站做网站做网站
  • 百度做网站续费费用网站开发的可行性
  • 电子商务网站建设效益分析如何才能做好品牌网站建设策划
  • 能打开各种网站的浏览器app文章目录wordpress
  • 网站注册页面html中国建设招标网网站
  • 云南网站设计海外直购网站建设方案书范文
  • 网站视频小程序商城多少钱