1. package com.yonge.lock;  

  2.   

  3. import java.util.Random;  

  4. import java.util.concurrent.ArrayBlockingQueue;  

  5.   

  6. /** 

  7.  * 需求:一个线程向一个固定大小的队列里面不停地存放数据,另一个线程不停的向这个队列里面取数据, 

  8.  * 当队列满了,还继续存放数据,此时出现阻塞,直到队列有空闲的位置; 

  9.  * 反之,当队列为空,还继续取数据,则也出现阻塞,知道队列中有数据为止 

  10.  * @author wb-gaoy 

  11.  * @version $Id: ArrayBlockingQueueTest.java,v 0.1 2012-1-6 上午10:54:11 wb-gaoy Exp $ 

  12.  */  

  13. public class ArrayBlockingQueueTest {  

  14.   

  15.     /** 

  16.      * @param args 

  17.      */  

  18.     public static void main(String[] args) {  

  19.   

  20.         //定义阻塞队列  

  21.         final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(5);  

  22.   

  23.         //开启一个put数据的线程  

  24.         new Thread(new Runnable() {  

  25.   

  26.             int i = 0;  

  27.   

  28.             @Override  

  29.             public void run() {  

  30.                 while (true) {  

  31.                     try {  

  32.                         System.out.println("ThreadName:" + Thread.currentThread().getName()  

  33.                                            + ":队列中已存在" + queue.size() + "元素");  

  34.                         if (queue.size() == 5) {  

  35.                             System.out.println("ThreadName:" + Thread.currentThread().getName()  

  36.                                                + ":队列已经满了,阻塞中...");  

  37.                         }  

  38.                         Thread.sleep((long) Math.random() * 10000);  

  39.                         i = new Random().nextInt(100);  

  40.                         System.out.println("ThreadName:" + Thread.currentThread().getName()  

  41.                                            + "准备存放的值为:" + i);  

  42.                         queue.put(i);  

  43.                         System.out.println("ThreadName:" + Thread.currentThread().getName()  

  44.                                            + "已存放的值为:" + i);  

  45.                     } catch (InterruptedException e) {  

  46.                         e.printStackTrace();  

  47.                     }  

  48.                 }  

  49.             }  

  50.         }, "A").start();  

  51.   

  52.         //开启一个take数据的线程  

  53.         new Thread(new Runnable() {  

  54.             @Override  

  55.             public void run() {  

  56.                 while (true) {  

  57.                     try {  

  58.                         System.out.println("ThreadName:" + Thread.currentThread().getName()  

  59.                                            + ":队列中已存在" + queue.size() + "元素");  

  60.                         if (queue.size() == 0) {  

  61.                             System.out.println("ThreadName:" + Thread.currentThread().getName()  

  62.                                                + ":队列已经空了,阻塞中...");  

  63.                         }  

  64.                         Thread.sleep((long) Math.random() * 10000);  

  65.                         System.out.println("ThreadName:" + Thread.currentThread().getName()  

  66.                                            + "获取的值为:" + queue.take());  

  67.                     } catch (InterruptedException e) {  

  68.                         e.printStackTrace();  

  69.                     }  

  70.                 }  

  71.             }  

  72.         }, "B").start();  

  73.     }  

  74.     /** 

  75.      * 总结:上面的代码没有原子性,打印的结果可能会出现偏差 

  76.      */  

  77. }