zoukankan      html  css  js  c++  java
  • 18.Java5阻塞队列的应用

    BlockingDeque 方法有四种形式,使用不同的方式处理无法立即满足但在将来某一时刻可能满足的操作:第一种方式抛出异常;第二种返回一个特殊值(nullfalse,具体取决于操作);第三种无限期阻塞当前线程,直至操作成功;第四种只阻塞给定的最大时间,然后放弃。下表中总结了这些方法:

    第一个元素(头部)
      抛出异常 特殊值 阻塞 超时期
    插入 addFirst(e) offerFirst(e) putFirst(e) offerFirst(e, time, unit)
    移除 removeFirst() pollFirst() takeFirst() pollFirst(time, unit)
    检查 getFirst() peekFirst() 不适用 不适用
    最后一个元素(尾部)
      抛出异常 特殊值 阻塞 超时期
    插入 addLast(e) offerLast(e) putLast(e) offerLast(e, time, unit)
    移除 removeLast() pollLast() takeLast() pollLast(time, unit)
    检查 getLast() peekLast() 不适用 不适用

    像所有 BlockingQueue 一样,BlockingDeque 是线程安全的,但不允许 null 元素,并且可能有(也可能没有)容量限制。 

     1 import java.util.concurrent.ArrayBlockingQueue;
     2 import java.util.concurrent.BlockingQueue;
     3 
     4 /**
     5  * 18.Java5阻塞队列的应用
     6  * 可阻塞的队列
     7  *     队列包含固定长度的队列和不固定长度的队列。
     8  *     什么是可阻塞队列,阻塞队列的作用与实际应用。阻塞队列的实现原理。
     9  *     ArrayBlockingQueue
    10  *         看BlockingQueue类的帮助文档,其中有各个方法的区别对比列表。
    11  *         只有put方法和take方法才具有阻塞功能。
    12  *     阻塞队列与Semaphore有些相似,但也不同,阻塞队列是一方存放数据,另一方释放数据
    13  *     Semaphore通常则是由同一方设置和释放信号量。
    14  * @author LiTaiQing
    15  *
    16  */
    17 public class BlockingQueueTest {
    18     public static void main(String[] args) {
    19         final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
    20         for(int i=0;i<2;i++){
    21             new Thread(){
    22                 public void run(){
    23                     while(true){
    24                         try {
    25                             Thread.sleep((long)(Math.random()*1000));
    26                             System.out.println(Thread.currentThread().getName() + "准备放数据!");                            
    27                             queue.put(1);
    28                             System.out.println(Thread.currentThread().getName() + "已经放了数据," +                             
    29                                         "队列目前有" + queue.size() + "个数据");
    30                         } catch (InterruptedException e) {
    31                             e.printStackTrace();
    32                         }
    33                     }
    34                 }
    35             }.start();
    36         }
    37         new Thread(){
    38             public void run(){
    39                 while(true){
    40                     try {
    41                         //将此处的睡眠时间分别改为100和1000,观察运行结果
    42                         Thread.sleep(1000);
    43                         System.out.println(Thread.currentThread().getName() + "准备取数据!");
    44                         queue.take();
    45                         System.out.println(Thread.currentThread().getName() + "已经取走数据," +                             
    46                                 "队列目前有" + queue.size() + "个数据");                    
    47                     } catch (InterruptedException e) {
    48                         e.printStackTrace();
    49                     }
    50                 }
    51             }
    52         }.start();            
    53     }
    54 }
     1 import java.util.Collections;
     2 import java.util.concurrent.ArrayBlockingQueue;
     3 import java.util.concurrent.BlockingQueue;
     4 
     5 public class BlockingQueueCommunication {
     6     public static void main(String[] args) {
     7         final Business business = new Business();
     8         new Thread(new Runnable() {
     9             @Override
    10             public void run() {
    11                 for (int i = 1; i <= 50; i++) {
    12                     business.sub(i);
    13                 }
    14 
    15             }
    16         }).start();
    17 
    18         for (int i = 1; i <= 50; i++) {
    19             business.main(i);
    20         }
    21     }
    22 
    23     static class Business {
    24 
    25         BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
    26         BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
    27 
    28         {
    29             Collections.synchronizedMap(null);
    30             try {
    31                 System.out.println("代码执行...");
    32                 queue2.put(1);
    33             } catch (InterruptedException e) {
    34                 e.printStackTrace();
    35             }
    36         }
    37 
    38         public void sub(int i) {
    39             try {
    40                 queue1.put(1);
    41             } catch (InterruptedException e) {
    42                 e.printStackTrace();
    43             }
    44             for (int j = 1; j <= 10; j++) {
    45                 System.out.println("sub thread sequece of " + j + ",loop of "
    46                         + i);
    47             }
    48             try {
    49                 queue2.take();
    50             } catch (InterruptedException e) {
    51                 e.printStackTrace();
    52             }
    53         }
    54 
    55         public void main(int i) {
    56             try {
    57                 queue2.put(1);
    58             } catch (InterruptedException e1) {
    59                 e1.printStackTrace();
    60             }
    61             for (int j = 1; j <= 100; j++) {
    62                 System.out.println("main thread sequece of " + j + ",loop of "
    63                         + i);
    64             }
    65             try {
    66                 queue1.take();
    67             } catch (InterruptedException e) {
    68                 e.printStackTrace();
    69             }
    70         }
    71     }
    72 }
  • 相关阅读:
    路飞学城Python-Day48
    路飞学城Python-Day46
    路飞学城Python-Day43
    路飞学城Python-Day42
    路飞学城Python-Day40(第四模块复习题)
    路飞学城Python-Day39(第四模块复习题)
    python小练习
    微信小程序常见错误及基本排除方法
    CSS文本样式
    小程序-广告轮播/控制属性
  • 原文地址:https://www.cnblogs.com/litaiqing/p/4651113.html
Copyright © 2011-2022 走看看