自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于Java阻塞隊(duì)列的搜索實(shí)例

開發(fā) 后端
隊(duì)列以一種先進(jìn)先出的方式管理數(shù)據(jù),如果你試圖向一個(gè)已經(jīng)滿了的阻塞隊(duì)列中添加一個(gè)元素,或是從一個(gè)空的阻塞隊(duì)列中移除一個(gè)元素,將導(dǎo)致線程阻塞。在多線程進(jìn)行合作時(shí),阻塞隊(duì)列是很有用的工具,工作者線程可以定期的把中間結(jié)果存到阻塞隊(duì)列中,而其他工作者線程把中間結(jié)果取出并在將來修改它們。

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

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

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

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

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

注意,這里不需要人任何顯示的線程同步。在這個(gè)程序中,我們使用隊(duì)列數(shù)據(jù)結(jié)構(gòu)作為一種同步機(jī)制。

  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.util.concurrent.*;  
  4.  
  5. public class BlockingQueueTest  
  6. {  
  7.    public static void main(String[] args)  
  8.    {  
  9.       Scanner in = new Scanner(System.in);  
  10.       System.out.print("Enter base directory (e.g. /usr/local/jdk1.6.0/src): ");  
  11.       String directory = in.nextLine();  
  12.       System.out.print("Enter keyword (e.g. volatile): ");  
  13.       String keyword = in.nextLine();  
  14.  
  15.       final int FILE_QUEUE_SIZE = 10;  
  16.       final int SEARCH_THREADS = 100;  
  17.  
  18.       BlockingQueue<File> queue = new ArrayBlockingQueue<File>(FILE_QUEUE_SIZE);  
  19.  
  20.       FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory));  
  21.       new Thread(enumerator).start();  
  22.       for (int i = 1; i <= SEARCH_THREADS; i++)  
  23.          new Thread(new SearchTask(queue, keyword)).start();  
  24.    }  
  25. }  
  26.  
  27. /**  
  28.  * This task enumerates all files in a directory and its subdirectories.  
  29.  */ 
  30. class FileEnumerationTask implements Runnable  
  31. {  
  32.    /**  
  33.     * Constructs a FileEnumerationTask.  
  34.     * @param queue the blocking queue to which the enumerated files are added  
  35.     * @param startingDirectory the directory in which to start the enumeration  
  36.     */ 
  37.    public FileEnumerationTask(BlockingQueue<File> queue, File startingDirectory)  
  38.    {  
  39.       this.queue = queue;  
  40.       this.startingDirectory = startingDirectory;  
  41.    }  
  42.  
  43.    public void run()  
  44.    {  
  45.       try 
  46.       {  
  47.          enumerate(startingDirectory);  
  48.          queue.put(DUMMY);  
  49.       }  
  50.       catch (InterruptedException e)  
  51.       {  
  52.       }  
  53.    }  
  54.  
  55.    /**  
  56.     * Recursively enumerates all files in a given directory and its subdirectories  
  57.     * @param directory the directory in which to start  
  58.     */ 
  59.    public void enumerate(File directory) throws InterruptedException  
  60.    {  
  61.       File[] files = directory.listFiles();  
  62.       for (File file : files)  
  63.       {  
  64.          if (file.isDirectory()) enumerate(file);  
  65.          else queue.put(file);  
  66.       }  
  67.    }  
  68.  
  69.    public static File DUMMY = new File("");  
  70.  
  71.    private BlockingQueue<File> queue;  
  72.    private File startingDirectory;  
  73. }  
  74.  
  75. /**  
  76.  * This task searches files for a given keyword.  
  77.  */ 
  78. class SearchTask implements Runnable  
  79. {  
  80.    /**  
  81.     * Constructs a SearchTask.  
  82.     * @param queue the queue from which to take files  
  83.     * @param keyword the keyword to look for  
  84.     */ 
  85.    public SearchTask(BlockingQueue<File> queue, String keyword)  
  86.    {  
  87.       this.queue = queue;  
  88.       this.keyword = keyword;  
  89.    }  
  90.  
  91.    public void run()  
  92.    {  
  93.       try 
  94.       {  
  95.          boolean done = false;  
  96.          while (!done)  
  97.          {  
  98.             File file = queue.take();  
  99.             if (file == FileEnumerationTask.DUMMY)  
  100.             {  
  101.                queue.put(file);  
  102.                done = true;  
  103.             }  
  104.             else search(file);              
  105.          }  
  106.       }  
  107.       catch (IOException e)  
  108.       {  
  109.          e.printStackTrace();  
  110.       }  
  111.       catch (InterruptedException e)  
  112.       {  
  113.       }        
  114.    }  
  115.  
  116.    /**  
  117.     * Searches a file for a given keyword and prints all matching lines.  
  118.     * @param file the file to search  
  119.     */ 
  120.    public void search(File file) throws IOException  
  121.    {  
  122.       Scanner in = new Scanner(new FileInputStream(file));  
  123.       int lineNumber = 0;  
  124.       while (in.hasNextLine())  
  125.       {  
  126.          lineNumber++;  
  127.          String line = in.nextLine().trim();  
  128.          if (line.contains(keyword)) System.out.printf("%s:%d    %s%n", file.getPath(), lineNumber, line);  
  129.       }  
  130.       in.close();  
  131.    }  
  132.  
  133.    private BlockingQueue<File> queue;  
  134.    private String keyword;  

原文鏈接:http://www.cnblogs.com/XL-Liang/archive/2012/06/13/2547929.html

責(zé)任編輯:林師授 來源: frogong的博客
相關(guān)推薦

2017-04-12 10:02:21

Java阻塞隊(duì)列原理分析

2020-11-25 14:28:56

DelayedWork

2020-11-19 07:41:51

ArrayBlocki

2020-11-24 09:04:55

PriorityBlo

2020-11-20 06:22:02

LinkedBlock

2025-01-14 00:00:00

Blocking隊(duì)列元素

2023-12-15 09:45:21

阻塞接口

2022-06-30 08:14:05

Java阻塞隊(duì)列

2023-12-30 13:47:48

Redis消息隊(duì)列機(jī)制

2022-06-30 14:31:57

Java阻塞隊(duì)列

2017-08-07 08:41:13

Java微服務(wù)構(gòu)建

2024-10-14 12:34:08

2021-06-04 18:14:15

阻塞非阻塞tcp

2009-02-01 10:54:00

MAC地址訪問控制

2012-02-23 15:36:51

IndexedDB

2009-08-17 17:53:07

RSS訂閱開發(fā)實(shí)例

2010-07-26 14:25:06

Widget開發(fā)

2010-07-26 14:44:47

Widget開發(fā)

2012-05-29 15:17:08

JavaWMIC

2011-05-17 17:51:43

SEO網(wǎng)站優(yōu)化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)