zoukankan      html  css  js  c++  java
  • JAVA多线程编程之生产者消费者模式

    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 = 10;
        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 = 0; i < N_CONSUMERS; i++)
                new Thread(new Indexer(queue)).start();
        }
    }

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

    数据来解析文件。

  • 相关阅读:
    解决Warning: mysql_connect(): Headers and client library minor version mismatch. 警告
    读取微博feed伪代码
    [待续]不为人知的PHP-SPL标准库
    封装pyMysql
    捉“客”记
    实现小程序插件自定义导航栏
    圆形与矩形的碰撞检测--Mr.Ember
    mpvue原理分析
    webpack学习--Mr.Ember
    原型链、继承--Mr.Ember
  • 原文地址:https://www.cnblogs.com/zhanglanyun/p/3304287.html
Copyright © 2011-2022 走看看