ArrayBlockingQueue

  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. }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章