Java中有一个BlockingQueue可以用来充当堵塞队列,下面是一个桌面搜索的设计

package net.jcip.examples;

import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.*; /**
* ProducerConsumer
* <p/>
* Producer and consumer tasks in a desktop search application
*
*/
public class ProducerConsumer {
static class FileCrawler implements Runnable {
private final BlockingQueue<File> fileQueue;
private final FileFilter fileFilter;
private final File root; public FileCrawler(BlockingQueue<File> fileQueue,
final FileFilter fileFilter,
File root) {
this.fileQueue = fileQueue;
this.root = root;
this.fileFilter = new FileFilter() {
public boolean accept(File f) {
return f.isDirectory() || fileFilter.accept(f);
}
};
} private boolean alreadyIndexed(File f) {
return false;
} public void run() {
try {
crawl(root);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} private void crawl(File root) throws InterruptedException {
File[] entries = root.listFiles(fileFilter);
if (entries != null) {
for (File entry : entries)
if (entry.isDirectory())
crawl(entry);
else if (!alreadyIndexed(entry))
fileQueue.put(entry);
}
}
} static class Indexer implements Runnable {
private final BlockingQueue<File> queue; public Indexer(BlockingQueue<File> queue) {
this.queue = queue;
} public void run() {
try {
while (true)
indexFile(queue.take());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} public void indexFile(File file) {
// Index the file...
};
} private static final int BOUND = ;
private static final int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); public static void startIndexing(File[] roots) {
BlockingQueue<File> queue = new LinkedBlockingQueue<File>(BOUND);
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return true;
}
}; for (File root : roots)
new Thread(new FileCrawler(queue, filter, root)).start(); for (int i = ; i < N_CONSUMERS; i++)
new Thread(new Indexer(queue)).start();
}
}

多个文件爬取线程充当生产者,不断的生产出数据来放到BlockingQueue中,然后由索引线程从BlockingQueue中得到

数据来解析文件。

最新文章

  1. ASP.NET Core 中文文档 第四章 MVC(4.3)过滤器
  2. [收藏]C++简单五子棋
  3. BigDecimal.ROUND_HALF_XXX的各种用法
  4. RBM Formula Deduction
  5. 2013长沙赛区现场赛 J - Josephina and RPG
  6. 文件上传插件uploadify详解
  7. LoadRunner执行过程报错“Failed to connect to server &quot;xxx.xxx.xxx.xxx:xx&quot;:[10060] connetion time out”
  8. UITableView添加静态背景.
  9. Android应用程序绑定服务(bindService)的过程源代码分析
  10. sqlHelper的增删改查
  11. Odoo安装
  12. http: server gave HTTP response to HTTPS client &amp; Get https://192.168.2.119/v2/: dial tcp 192.168.2.119:443: getsockopt: connection refused
  13. java jar 包加载文件问题
  14. js增加、删除、替换DOM对象
  15. 数位dp poj1850
  16. grid网格的流动一
  17. HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]
  18. Java下载https文件上传到阿里云oss服务器
  19. .NET开源工作流RoadFlow-流程运行-工作委托
  20. NoSQL入门第三天——Redis配置文件与持久化

热门文章

  1. AngularJS快速入门指南05:控制器
  2. JavaScript toFixed function Not Rouding
  3. windows下安装mysql压缩包版[转]
  4. paip.提升效率---request自动绑定domain object
  5. paip.python连接mysql最佳实践o4
  6. XML的简单学习
  7. UItableView嵌套UICollectionView
  8. 利用Httponly提升web应用程序安全性
  9. U盘启动笔记本无法安装Win7问题和解决
  10. C#Winform程序如何发布并自动升级(图解)