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