Java中使用阻塞队列控制线程集实例

 更新时间:2015年01月15日 11:08:18   投稿:junjie  
这篇文章主要介绍了Java控制阻塞队列线程集实例,本文用一个程序展示了如何使用阻塞队列来控制线程集,程序功能是在一个目录及它的所有子目录下搜索所有文件,打印出包含指定关键字的文件列表,需要的朋友可以参考下

队列以一种先进先出的方式管理数据。如果你试图向一个已经满了的阻塞队列中添加一个元素,或是从一个空的阻塞队列中移除一个元素,将导致线程阻塞。在多线程进行合作时,阻塞队列是很有用的工具。工作者线程可以定期的把中间结果存到阻塞队列中。而其他工作者线程把中间结果取出并在将来修改它们。队列会自动平衡负载。如果第一个线程集运行的比第二个慢,则第二个线程集在等待结果时就会阻塞。如果第一个线程集运行的快,那么它将等待第二个线程集赶上来。

下面的程序展示了如何使用阻塞队列来控制线程集。程序在一个目录及它的所有子目录下搜索所有文件,打印出包含指定关键字的文件列表。

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

相关文章

  • Mybatis-Plus 3.5.12 分页拦截器消失的问题及快速解决方法

    Mybatis-Plus 3.5.12 分页拦截器消失的问题及快速解决方法

    作为Java开发者,我们都爱用Mybatis-Plus简化CRUD操作,尤其是它的分页功能,几行代码就能搞定复杂的分页查询,本文给大家介绍Mybatis-Plus3.5.12分页拦截器消失的问题快速解决方法,感兴趣的朋友跟随小编一起看看吧
    2025-08-08
  • 聊聊SpringCloud和SpringCloudAlibaba的区别

    聊聊SpringCloud和SpringCloudAlibaba的区别

    这篇文章主要介绍了SpringCloud和SpringCloudAlibaba的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java哈希表的概念及实现完整代码

    Java哈希表的概念及实现完整代码

    这篇文章主要介绍了Java哈希表的概念及实现的相关资料,哈希表是一种高效查找数据的结构,通过哈希函数将关键字映射到数组的索引位置,当发生冲突时,可以通过闭散列或开散列(链地址法)来解决,需要的朋友可以参考下
    2024-11-11
  • Spring Boot MQTT Too many publishes in progress错误的解决方案

    Spring Boot MQTT Too many publishes in progress错误的解决方

    本文介绍Spring Boot MQTT Too many publishes in progress错误的解决方案,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-07-07
  • RabbitMq中channel接口的几种常用参数详解

    RabbitMq中channel接口的几种常用参数详解

    这篇文章主要介绍了RabbitMq中channel接口的几种常用参数详解,RabbitMQ 不会为未确认的消息设置过期时间,它判断此消息是否需要重新投递给消费者的唯一依据是消费该消息的消费者连接是否己经断开,需要的朋友可以参考下
    2023-08-08
  • Java之循环结构、break、continue用法及说明

    Java之循环结构、break、continue用法及说明

    本文介绍了Java中的三种循环语句:while、for和do-while,并详细讲解了它们的使用方法和应用场景,通过实例练习,读者可以掌握循环语句的编写技巧
    2025-10-10
  • JAVA中通过Redis实现延时任务demo实例

    JAVA中通过Redis实现延时任务demo实例

    Redis在2.0版本时引入了发布订阅(pub/sub)功能,在发布订阅中有一个channel(频道),与消息队列中的topic(主题)类似,可以通过redis的发布订阅者模式实现延时任务功能,实例中会议室预约系统,用户预约管理员审核后生效,如未审批,需要自动变超期未处理,使用延时任务
    2024-08-08
  • 解决Java中的java.io.IOException: Broken pipe问题

    解决Java中的java.io.IOException: Broken pipe问题

    这篇文章主要介绍了解决Java中 java.io.IOException: Broken pipe的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Java使用自定义注解实现函数测试功能示例

    Java使用自定义注解实现函数测试功能示例

    这篇文章主要介绍了Java使用自定义注解实现函数测试功能,结合实例形式分析了java自定义注解在函数测试过程中相关功能、原理与使用技巧,需要的朋友可以参考下
    2019-10-10
  • JAVA利用接口实现多继承问题的代码实操演示

    JAVA利用接口实现多继承问题的代码实操演示

    Java语言并不支持多继承,这是由于多继承会带来许多复杂的问题,例如"菱形问题"等,下面这篇文章主要给大家介绍了关于JAVA利用接口实现多继承问题的相关资料,需要的朋友可以参考下
    2024-03-03

最新评论