jdk1.4 構建 java多線程,併發設計框架 (三)

 啓動一個線程,用來跟蹤隊排裏的客戶請求
  1. /**
  2.  * 隊列服務.用來查看線程隊排中的請求.
  3.  * 啓動一個線程,用來跟蹤隊排裏的客戶請求.
  4.  * 如果隊中有客戶的請求,則創建一個新線程獨立處理,<br>
  5.  * @author guishuanglin 2008-11-3
  6.  * 
  7.  */
  8. public class ConcurrentQueueService extends Thread {
  9.     //IConcurrentCall
  10.     private ConcurrentQueue _queue;
  11.     
  12.     /**
  13.      * 啓動一個線程來跟蹤堆中需要處理的對象.
  14.      * @param queue
  15.      */
  16.     public ConcurrentQueueService(ConcurrentQueue queue) {
  17.         _queue = queue;
  18.         start();
  19.     }
  20.     /**
  21.      * 入隊
  22.      * @param call
  23.      */
  24.     public void enQueue(IConcurrentCall target) {
  25.         _queue.enQueue(target);
  26.     }
  27.     
  28.     /**
  29.      * 單一線程跟蹤隊排請求,並把每個請求交給一個新線程處理.
  30.      * 此處是否需要創建新的線程處理,有待實際需要,因爲這將又產生線程併發問題.
  31.      * @param call
  32.      */
  33.     public void run() {
  34.         Runnable call = null;
  35.         while (true) {
  36.             call = (Runnable)_queue.deQueue();
  37.             if(call != null){
  38.                 //可以不使用獨立的線程處理,直接順序處理.
  39.                 call.run();
  40.                 //new Thread(call).start();
  41.             }
  42.         }
  43.     }
  44. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章