• java 5并发中的阻塞队列ArrayBlockingQueue的使用以及案例实现


    演示一个阻塞队列的使用

     1 public class BlockingQueueTest
     2 {
     3     public static void main(String[] args)
     4     {
     5         //创建一个包含三个元素的阻塞队列
     6         final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
     7         for (int i = 0; i < 2; i++)
     8         {
     9             new Thread(new Runnable()
    10             {
    11                 @Override
    12                 public void run()
    13                 {
    14                     while(true){
    15                         try
    16                         {
    17                             Thread.sleep(1000);
    18                             System.out.println("线程"+Thread.currentThread().getName()+" 准备放数据了");
    19                             queue.put(new Random().nextInt(100));
    20                             System.out.println("线程"+Thread.currentThread().getName()+" 已经放完数据了,目前队列有"+queue.size()+"个数据");
    21                         } catch (InterruptedException e)
    22                         {
    23                             e.printStackTrace();
    24                         }
    25                     }
    26                 }
    27             }).start();
    28             
    29         }
    30         
    31         new Thread(new Runnable()
    32         {
    33             @Override
    34             public void run()
    35             {
    36                 while(true){
    37                     try
    38                     {
    39                         //将此处的睡眠时间改为200和2000 分别观察结果
    40                         Thread.sleep(2000);
    41                         System.out.println("线程"+Thread.currentThread().getName()+" 准备取数据了");
    42                         queue.take();
    43                         System.out.println("线程"+Thread.currentThread().getName()+" 已经取完数据了,目前队列有"+queue.size()+"个数据");
    44                     } catch (InterruptedException e)
    45                     {
    46                         e.printStackTrace();
    47                     }
    48                 }
    49             }
    50         }).start();
    51     }
    52 }

    部分运行结果如下,

    线程Thread-1 准备放数据了
    线程Thread-0 准备放数据了
    线程Thread-1 已经放完数据了,目前队列有1个数据
    线程Thread-0 已经放完数据了,目前队列有2个数据
    线程Thread-2 准备取数据了
    线程Thread-2 已经取完数据了,目前队列有1个数据
    线程Thread-1 准备放数据了
    线程Thread-0 准备放数据了
    线程Thread-1 已经放完数据了,目前队列有2个数据
    线程Thread-0 已经放完数据了,目前队列有3个数据
    线程Thread-1 准备放数据了
    线程Thread-0 准备放数据了
    线程Thread-2 准备取数据了
    线程Thread-2 已经取完数据了,目前队列有2个数据
    线程Thread-1 已经放完数据了,目前队列有3个数据
    线程Thread-1 准备放数据了
    线程Thread-2 准备取数据了
    线程Thread-2 已经取完数据了,目前队列有2个数据
    线程Thread-0 已经放完数据了,目前队列有3个数据
    线程Thread-0 准备放数据了
    线程Thread-2 准备取数据了
    线程Thread-2 已经取完数据了,目前队列有2个数据
    线程Thread-1 已经放完数据了,目前队列有3个数据
    线程Thread-1 准备放数据了
    线程Thread-2 准备取数据了
    线程Thread-2 已经取完数据了,目前队列有2个数据
    线程Thread-0 已经放完数据了,目前队列有3个数据
    线程Thread-0 准备放数据了
    View Code

    * 面试题
    * 需求:
    * 子线程循环输出10次
    * 接着主线程循环输出100次
    * 接着又回到子线程执行10次
    * 再回到主线程执行100次
    * 如此往复50次 请写出程序
    * 此处使用阻塞队列来实现

     1 public class BlockingQueueCommunication
     2 {
     3     public static void main(String[] args) throws InterruptedException
     4     {
     5         final Core core = new Core();
     6         //子线程
     7         new Thread(
     8                 new Runnable()
     9                 {
    10                     @Override
    11                     public void run()
    12                     {
    13                         for (int i = 1; i <= 50;i++)
    14                         {
    15                             core.SubMethod(i);
    16                         }    
    17                     }
    18                 }
    19         ).start();
    20         
    21         //主线程
    22         for (int i = 1; i <= 50; i++)
    23         {
    24             core.MainMethod(i);
    25         }
    26     }
    27     
    28     /**
    29      * 创建一个静态的类
    30      * 将核心的业务逻辑的方法放在这里
    31      * 体现了高内聚的特点
    32      * @author huang.jf
    33      *
    34      */
    35     static class Core{
    36         //创建两个阻塞队列  都只能装一个元素
    37         //实现的效果就是q1存,q2取,q2存,q1取
    38         //用两个具有一个空间的队列来实现同步通知的功能
    39         private BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
    40         private BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
    41         
    42         //匿名构造方法 也叫非静态代码块
    43         //当创建对象时 该代码块先被执行
    44         //保证初始化,在queue2队列中包含一个元素
    45         {
    46             try
    47             {
    48                 queue2.put(1);
    49             } catch (InterruptedException e)
    50             {
    51                 e.printStackTrace();
    52             }
    53         }
    54         
    55         
    56         //子线程调用 循环输出10次
    57         public  void SubMethod(int j){
    58             try
    59             {
    60                 queue1.put(1);//向q1中存入一个值
    61             } catch (InterruptedException e)
    62             {
    63                 e.printStackTrace();
    64             }
    65             for (int i = 1; i <= 10; i++)
    66             {
    67                 System.out.println("this is sub thread..."+i+"......."+j);
    68             }
    69             try
    70             {
    71                 queue2.take();//取出q2中的一个值
    72             } catch (InterruptedException e)
    73             {
    74                 e.printStackTrace();
    75             }
    76         }
    77         //主线程调用循环输出一百次
    78         public  void MainMethod(int j){
    79             try
    80             {
    81                 queue2.put(1);//一开始queue2队列会被阻塞,因为初始化的时候已经有一个值
    82             } catch (InterruptedException e)
    83             {
    84                 e.printStackTrace();
    85             }
    86             for (int i = 1; i <= 100; i++)
    87             {
    88                 System.out.println("this is main thread..."+i+"......"+j);
    89             }
    90             try
    91             {
    92                 queue1.take();
    93             } catch (InterruptedException e)
    94             {
    95                 e.printStackTrace();
    96             }
    97         }
    98     }
    99 }

    部分运行结果如下:

    this is sub thread...1.......49
    this is sub thread...2.......49
    this is sub thread...3.......49
    this is sub thread...4.......49
    this is sub thread...5.......49
    this is sub thread...6.......49
    this is sub thread...7.......49
    this is sub thread...8.......49
    this is sub thread...9.......49
    this is sub thread...10.......49
    this is main thread...1......49
    this is main thread...2......49
    this is main thread...3......49
    this is main thread...4......49
    this is main thread...5......49
    this is main thread...6......49
    this is main thread...7......49
    this is main thread...8......49
    this is main thread...9......49
    this is main thread...10......49
    this is main thread...11......49
    this is main thread...12......49
    this is main thread...13......49
    this is main thread...14......49
    this is main thread...15......49
    this is main thread...16......49
    this is main thread...17......49
    this is main thread...18......49
    this is main thread...19......49
    this is main thread...20......49
    this is main thread...21......49
    this is main thread...22......49
    this is main thread...23......49
    this is main thread...24......49
    this is main thread...25......49
    this is main thread...26......49
    this is main thread...27......49
    this is main thread...28......49
    this is main thread...29......49
    this is main thread...30......49
    this is main thread...31......49
    this is main thread...32......49
    this is main thread...33......49
    this is main thread...34......49
    this is main thread...35......49
    this is main thread...36......49
    this is main thread...37......49
    this is main thread...38......49
    this is main thread...39......49
    this is main thread...40......49
    this is main thread...41......49
    this is main thread...42......49
    this is main thread...43......49
    this is main thread...44......49
    this is main thread...45......49
    this is main thread...46......49
    this is main thread...47......49
    this is main thread...48......49
    this is main thread...49......49
    this is main thread...50......49
    this is main thread...51......49
    this is main thread...52......49
    this is main thread...53......49
    this is main thread...54......49
    this is main thread...55......49
    this is main thread...56......49
    this is main thread...57......49
    this is main thread...58......49
    this is main thread...59......49
    this is main thread...60......49
    this is main thread...61......49
    this is main thread...62......49
    this is main thread...63......49
    this is main thread...64......49
    this is main thread...65......49
    this is main thread...66......49
    this is main thread...67......49
    this is main thread...68......49
    this is main thread...69......49
    this is main thread...70......49
    this is main thread...71......49
    this is main thread...72......49
    this is main thread...73......49
    this is main thread...74......49
    this is main thread...75......49
    this is main thread...76......49
    this is main thread...77......49
    this is main thread...78......49
    this is main thread...79......49
    this is main thread...80......49
    this is main thread...81......49
    this is main thread...82......49
    this is main thread...83......49
    this is main thread...84......49
    this is main thread...85......49
    this is main thread...86......49
    this is main thread...87......49
    this is main thread...88......49
    this is main thread...89......49
    this is main thread...90......49
    this is main thread...91......49
    this is main thread...92......49
    this is main thread...93......49
    this is main thread...94......49
    this is main thread...95......49
    this is main thread...96......49
    this is main thread...97......49
    this is main thread...98......49
    this is main thread...99......49
    this is main thread...100......49
    this is sub thread...1.......50
    this is sub thread...2.......50
    this is sub thread...3.......50
    this is sub thread...4.......50
    this is sub thread...5.......50
    this is sub thread...6.......50
    this is sub thread...7.......50
    this is sub thread...8.......50
    this is sub thread...9.......50
    this is sub thread...10.......50
    this is main thread...1......50
    this is main thread...2......50
    this is main thread...3......50
    this is main thread...4......50
    this is main thread...5......50
    this is main thread...6......50
    this is main thread...7......50
    this is main thread...8......50
    this is main thread...9......50
    this is main thread...10......50
    this is main thread...11......50
    this is main thread...12......50
    this is main thread...13......50
    this is main thread...14......50
    this is main thread...15......50
    this is main thread...16......50
    this is main thread...17......50
    this is main thread...18......50
    this is main thread...19......50
    this is main thread...20......50
    this is main thread...21......50
    this is main thread...22......50
    this is main thread...23......50
    this is main thread...24......50
    this is main thread...25......50
    this is main thread...26......50
    this is main thread...27......50
    this is main thread...28......50
    this is main thread...29......50
    this is main thread...30......50
    this is main thread...31......50
    this is main thread...32......50
    this is main thread...33......50
    this is main thread...34......50
    this is main thread...35......50
    this is main thread...36......50
    this is main thread...37......50
    this is main thread...38......50
    this is main thread...39......50
    this is main thread...40......50
    this is main thread...41......50
    this is main thread...42......50
    this is main thread...43......50
    this is main thread...44......50
    this is main thread...45......50
    this is main thread...46......50
    this is main thread...47......50
    this is main thread...48......50
    this is main thread...49......50
    this is main thread...50......50
    this is main thread...51......50
    this is main thread...52......50
    this is main thread...53......50
    this is main thread...54......50
    this is main thread...55......50
    this is main thread...56......50
    this is main thread...57......50
    this is main thread...58......50
    this is main thread...59......50
    this is main thread...60......50
    this is main thread...61......50
    this is main thread...62......50
    this is main thread...63......50
    this is main thread...64......50
    this is main thread...65......50
    this is main thread...66......50
    this is main thread...67......50
    this is main thread...68......50
    this is main thread...69......50
    this is main thread...70......50
    this is main thread...71......50
    this is main thread...72......50
    this is main thread...73......50
    this is main thread...74......50
    this is main thread...75......50
    this is main thread...76......50
    this is main thread...77......50
    this is main thread...78......50
    this is main thread...79......50
    this is main thread...80......50
    this is main thread...81......50
    this is main thread...82......50
    this is main thread...83......50
    this is main thread...84......50
    this is main thread...85......50
    this is main thread...86......50
    this is main thread...87......50
    this is main thread...88......50
    this is main thread...89......50
    this is main thread...90......50
    this is main thread...91......50
    this is main thread...92......50
    this is main thread...93......50
    this is main thread...94......50
    this is main thread...95......50
    this is main thread...96......50
    this is main thread...97......50
    this is main thread...98......50
    this is main thread...99......50
    this is main thread...100......50
    View Code
  • 相关阅读:
    BZOJ 3744 Gty的妹子序列 分块+树状数组+主席树
    BZOJ 3744 Gty的妹子序列 分块+树状数组
    51nod 1850 抽卡大赛 (十二省联考模测) DP
    BZOJ 4127: Abs (树链剖分 线段树求区间绝对值之和 带区间加法)
    BZOJ 2157: 旅游 (树链剖分+线段树)
    BZOJ 2836: 魔法树 (树链剖分+线段树)
    BZOJ 3531: [Sdoi2014]旅行 (树剖+动态开点线段树)
    树状数组彻底入门,算法小白都看得懂的超详细解析
    点双联通分量(BCC)的正确姿势
    uoj30【CF Round #278】Tourists(圆方树+树链剖分+可删除堆)
  • 原文地址:https://www.cnblogs.com/little-fly/p/7601477.html
Copyright © 2020-2023  润新知