国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中使用阻塞隊列控制線程集實例

2019-11-26 15:19:10
字體:
來源:轉載
供稿:網友

隊列以一種先進先出的方式管理數據。如果你試圖向一個已經滿了的阻塞隊列中添加一個元素,或是從一個空的阻塞隊列中移除一個元素,將導致線程阻塞。在多線程進行合作時,阻塞隊列是很有用的工具。工作者線程可以定期的把中間結果存到阻塞隊列中。而其他工作者線程把中間結果取出并在將來修改它們。隊列會自動平衡負載。如果第一個線程集運行的比第二個慢,則第二個線程集在等待結果時就會阻塞。如果第一個線程集運行的快,那么它將等待第二個線程集趕上來。

下面的程序展示了如何使用阻塞隊列來控制線程集。程序在一個目錄及它的所有子目錄下搜索所有文件,打印出包含指定關鍵字的文件列表。

java.util.concurrent包提供了阻塞隊列的4個變種:LinkedBlockingQueue、ArrayBlockingQueue、PriorityBlockingQueue和DelayQueue。我們用的是ArrayBlockingQueue。ArrayBlockingQueue在構造時需要給定容量,并可以選擇是否需要公平性。如果公平參數被設置了,等待時間最長的線程會優先得到處理。通常,公平性會使你在性能上付出代價,只有在的確非常需要的時候再使用它。

生產者線程枚舉在所有子目錄下的所有文件并把它們放到一個阻塞隊列中。這個操作很快,如果隊列沒有設上限的話,很快它就包含了沒有找到的文件。

我們同時還啟動了大量的搜索線程。每個搜索線程從隊列中取出一個文件,打開它,打印出包含關鍵字的所有行,然后取出下一個文件。我們使用了一個小技巧來在工作結束后終止線程。為了發出完成信號,枚舉線程把一個虛擬對象放入隊列。(這類似于在行李輸送帶上放一個寫著“最后一個包”的虛擬包。)當搜索線程取到這個虛擬對象時,就將其放回并終止。

注意,這里不需要人任何顯示的線程同步。在這個程序中,我們使用隊列數據結構作為一種同步機制。

復制代碼 代碼如下:

import java.io.*; 
import java.util.*; 
import java.util.concurrent.*; 

public class BlockingQueueTest 

   public static void main(String[] args) 
   { 
      Scanner in = new Scanner(System.in); 
      System.out.print("Enter base directory (e.g. /usr/local/jdk1.6.0/src): "); 
      String directory = in.nextLine(); 
      System.out.print("Enter keyword (e.g. volatile): "); 
      String keyword = in.nextLine(); 

      final int FILE_QUEUE_SIZE = 10; 
      final int SEARCH_THREADS = 100; 

      BlockingQueue<File> queue = new ArrayBlockingQueue<File>(FILE_QUEUE_SIZE); 

      FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory)); 
      new Thread(enumerator).start(); 
      for (int i = 1; i <= SEARCH_THREADS; i++) 
         new Thread(new SearchTask(queue, keyword)).start(); 
   } 

/** 
 * This task enumerates all files in a directory and its subdirectories. 
 */
class FileEnumerationTask implements Runnable 

   /** 
    * Constructs a FileEnumerationTask. 
    * @param queue the blocking queue to which the enumerated files are added 
    * @param startingDirectory the directory in which to start the enumeration 
    */
   public FileEnumerationTask(BlockingQueue<File> queue, File startingDirectory) 
   { 
      this.queue = queue; 
      this.startingDirectory = startingDirectory; 
   } 

   public void run() 
   { 
      try
      { 
         enumerate(startingDirectory); 
         queue.put(DUMMY); 
      } 
      catch (InterruptedException e) 
      { 
      } 
   } 

   /** 
    * Recursively enumerates all files in a given directory and its subdirectories 
    * @param directory the directory in which to start 
    */
   public void enumerate(File directory) throws InterruptedException 
   { 
      File[] files = directory.listFiles(); 
      for (File file : files) 
      { 
         if (file.isDirectory()) enumerate(file); 
         else queue.put(file); 
      } 
   } 

   public static File DUMMY = new File(""); 

   private BlockingQueue<File> queue; 
   private File startingDirectory; 

/** 
 * This task searches files for a given keyword. 
 */
class SearchTask implements Runnable 

   /** 
    * Constructs a SearchTask. 
    * @param queue the queue from which to take files 
    * @param keyword the keyword to look for 
    */
   public SearchTask(BlockingQueue<File> queue, String keyword) 
   { 
      this.queue = queue; 
      this.keyword = keyword; 
   } 

   public void run() 
   { 
      try
      { 
         boolean done = false; 
         while (!done) 
         { 
            File file = queue.take(); 
            if (file == FileEnumerationTask.DUMMY) 
            { 
               queue.put(file); 
               done = true; 
            } 
            else search(file);             
         } 
      } 
      catch (IOException e) 
      { 
         e.printStackTrace(); 
      } 
      catch (InterruptedException e) 
      { 
      }       
   } 

   /** 
    * Searches a file for a given keyword and prints all matching lines. 
    * @param file the file to search 
    */
   public void search(File file) throws IOException 
   { 
      Scanner in = new Scanner(new FileInputStream(file)); 
      int lineNumber = 0; 
      while (in.hasNextLine()) 
      { 
         lineNumber++; 
         String line = in.nextLine().trim(); 
         if (line.contains(keyword)) System.out.printf("%s:%d    %s%n", file.getPath(), lineNumber, line); 
      } 
      in.close(); 
   } 

   private BlockingQueue<File> queue; 
   private String keyword; 
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聊城市| 门源| 莱芜市| 湖南省| 石阡县| 和田市| 且末县| 银川市| 佛坪县| 永胜县| 英德市| 无为县| 巨野县| 漯河市| 遵化市| 石河子市| 楚雄市| 清镇市| 栖霞市| 丽水市| 闵行区| 盐津县| 中阳县| 疏勒县| 肥西县| 新邵县| 城固县| 星座| 佳木斯市| 新丰县| 藁城市| 灯塔市| 金湖县| 贵港市| 体育| 安康市| 正安县| 闵行区| 布拖县| 遂昌县| 岳阳市|