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中得到

数据来解析文件。

JAVA多线程编程之生产者消费者模式的更多相关文章

  1. java 多线程 22 :生产者/消费者模式 进阶 利用await()/signal()实现

    java多线程15 :wait()和notify() 的生产者/消费者模式 在这一章已经实现了  wait/notify 生产消费模型 利用await()/signal()实现生产者和消费者模型 一样 ...

  2. 2.5多线程(Java学习笔记)生产者消费者模式

    一.什么是生产者消费者模式 生产者生产数据存放在缓冲区,消费者从缓冲区拿出数据处理. 可能大家会问这样有何好处? 1.解耦 由于有了缓冲区,生产者和消费者之间不直接依赖,耦合度降低,便于程序拓展和维护 ...

  3. Java多线程学习笔记--生产消费者模式

    实际开发中,我们经常会接触到生产消费者模型,如:Android的Looper相应handler处理UI操作,Socket通信的响应过程.数据缓冲区在文件读写应用等.强大的模型框架,鉴于本人水平有限目前 ...

  4. 多线程学习之三生产者消费者模式Guarded Suspension

    Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者                1.1该 ...

  5. JAVA多线程经典问题 -- 生产者 消费者

    工作2年多来一直也没有计划写自己的技术博客,最近辞职在家翻看<thingking in JAVA>,偶尔看到了生产者与消费者的一个经典的多线程同步问题.本人在工作中很少使用到多线程以及高并 ...

  6. Java多线程-并发协作(生产者消费者模型)

    对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一样,Hello World!都是最经典的例子. 实际上,准确说应该是“生产者-消费者-仓储”模型,离开了仓 ...

  7. Java多线程14:生产者/消费者模型

    什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...

  8. JAVA多线程经典问题 -- 生产者 消费者 同步队列实现方法

    在JAVASE5 中的java.util.concurrent.BlockingQueue支持,BlockingQueue是一个接口但是我们通常可以使用LinkedBlockingQueue,它是一个 ...

  9. Java多线程编程中Future模式的详解

    Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Futu ...

随机推荐

  1. Redis教程(六):Sorted-Sets数据类型

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/133.html 一.概述: Sorted-Sets和Sets类型极为相似, ...

  2. highchart 设置双Y轴坐标 双x轴坐标方法

    我们的图表一旦引入了两种不同单位或者数量级相差很大的数据以后,这时候需要两种坐标对其进行计量. 下面以设置双Y轴为例, y轴坐标的参数设置成: yAxis: [{ title: { text: '坐标 ...

  3. asp.net将内容导出到Excel,Table表格数据(html)导出EXCEL

    代码: /// <summary> /// HTML Table表格数据(html)导出EXCEL /// </summary> /// <param name=&quo ...

  4. 重学JAVA基础(六):多线程的同步

    1.synchronized关键字 /** * 同步关键字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */ public class SyncT ...

  5. VC中打开对话框选择文件和文件夹

    1.选择文件               CFileDialogdlg(true, NULL, NULL, NULL, "所有文件 | *.*", this);           ...

  6. Linux操作系统shell与函数详解

    shell和函数的定义 1. linux  shell 函数 将一组命令集或语句形成一个可用的块, 这些语句块称为函数. 2. shell  函数的组成 函数名:函数名字,注意一个脚本中函数名要唯一, ...

  7. 01、手把手Android攻城入门

    1.Android开发环境搭建: Eclipse Java EE IDE + ADT-23.0.6 + android-sdk-21-with-sdk-manager-r23.0.2 (安卓5.0)+ ...

  8. 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment

    http://stackoverflow.com/questions/10849552/update-viewpager-dynamically if you want to switch out t ...

  9. java string转为xml

    一.使用最原始的javax.xml.parsers,标准的jdk api // 字符串转XML String xmlStr = \"......\"; StringReader s ...

  10. 【Vegas原创】RHEL6多界面切换方法

    CTRL+ALT+Fn(n=1-6) F1,是图形化界面 F2-F6,是字符型界面. 每个界面可以干不同的事情,很牛叉.